go调试器:delve_使用VS Code和Delve调试Go

本文介绍了如何使用Microsoft的VS Code编辑器和Derek Parker的Delve工具调试Go程序。首先需要安装Go的VS Code扩展和配置$GOPATH。接着在不同操作系统上安装Delve,然后设置调试器配置。调试器提供了本地和远程调试功能,包括查看局部和全局变量、监视特定变量、调用堆栈和断点。通过调试Go标准库,可以深入了解其内部工作原理。
摘要由CSDN通过智能技术生成

go调试器:delve

VS Code from Microsoft is my favorite code editor. I used tons of editors in the last 20 years and this is the best by far.

我最喜欢的代码编辑器是Microsoft的VS Code。 在过去的20年中,我使用了大量的编辑器,这是迄今为止最好的。

It’s Open Source, fast, even on my not-so-recent MacBook Pro, it never crashed in the middle of something, it’s hackable and yet works great out of the box.

它是开源的 ,速度很快,即使在我不是那么新近的MacBook Pro上,它也绝不会崩溃,它可以被黑客入侵,但是开箱即用。

It’s all amazing, but how do you debug Go programs? You might think you need a heavy IDE for that, but that’s not true. It’s super easy, with Delve, made by Derek Parker.

一切都很棒,但是如何调试Go程序呢? 您可能会认为您需要一个笨重的IDE,但这不是事实。 使用Derek Parker制作的Delve超级简单。

First of course you need to install the official Go VS Code extension, and make sure $GOPATH is configured.

当然,首先您需要安装官方的Go VS Code扩展 ,并确保已配置$ GOPATH

Once this is installed, on Linux/Win you just need to execute the command Go: Install/Update Tools. On a Mac, you need to download Delve through Brew, by typing brew install go-delve/delve/delve in your favorite shell (which should be zsh!) - the docs talk about self-signing, but Brew handles this for you.

安装完成后,在Linux / Win上,您只需执行命令Go: Install/Update Tools 。 在Mac上,您需要通过Brew下载Delve,方法是在您喜欢的外壳程序(应该是zsh !)中键入brew install go-delve/delve/delve (文档应该是zsh !)-文档讨论自签名,但是Brew为您处理此问题。

Once you’re done, setup the debugger configuration.

完成后,设置调试器配置。

Click the VS Code debug menu and click “Start debugging”, or just press F5.

单击VS Code 调试菜单 ,然后单击“开始调试”,或仅按F5键。

VS Code puts the debug information in the .vscode/launch.json file in the root of your workspace. This will will be auto-generated and should work out of the box for local debugging:

VS Code将调试信息放入工作区根目录的.vscode/launch.json文件中。 这将是自动生成的,应该可以立即进行本地调试:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${fileDirname}",
            "env": {},
            "args": [],
            "showLog": true
        }
    ]
}

As per the docs, the mode parameter can be set to:

根据文档,mode参数可以设置为:

debug to compile and debug the current program test to debug tests (to debug a single test, pass -test.run and the Test name as args) exec to run a pre-built binary specified in program, for example "program":"${workspaceRoot}/mybin". remote to debug remotely (refer to the official documentation on Remote Debugging)

debug以编译和调试当前程序test以调试测试(以调试单个测试,将-test.run和Test名称作为args传递) exec以运行程序中指定的预构建二进制文件,例如"program":"${workspaceRoot}/mybin"remote以进行remote调试(请参阅远程调试官方文档 )

Having trouble? Here are some common pitfalls.

遇到麻烦? 这是一些常见的陷阱

Once this is done, Delve is ready to run!

一旦完成,Delve就可以运行了!

Go Debugging with VS Code and Delve

You can see that this is a pretty standard debugger, with local and global variables inspection, the option to watch specific variables, the call stack visualization and breakpoints.

您会看到这是一个非常标准的调试器,它具有局部和全局变量检查监视特定变量的选项, 调用堆栈可视化断点

What I especially like about debugging Go, and stepping into lib calls is that the standard library is very clean, well documented and readable, and by debuggging you learn a lot of things on how things work internally as well.

我特别喜欢调试Go并进入lib调用的地方是,标准库非常干净,文档齐全且可读性强,并且通过调试,您还了解了很多关于内部工作方式的知识。

Happy debugging!

调试愉快!

翻译自: https://flaviocopes.com/go-debugging-vscode-delve/

go调试器:delve

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值