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](https://flaviocopes.com/go-debugging-vscode-delve/go-debugging-vscode-delve.jpg)
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!
调试愉快!
go调试器:delve