安装 delve
windows#
go get -u github.com/go-delve/delve/cmd/dlv
linux#
go get -u github.com/go-delve/delve/cmd/dlv
$ git clone https://github.com/go-delve/delve.git $GOPATH/src/github.com/go-delve/delve
$ cd $GOPATH/src/github.com/go-delve/delve
$ make install
GO15VENDOREXPERIMENT=1
OSX#
$ go get -u github.com/go-delve/delve/cmd/dlv
执行上述代码前,确保你电脑上有编译工具
设置 launch.json 配置文件ctrl+shift+pDebug: Open launch.jsonlaunch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": []
}
]
}
常见属性如下
autodebugremotetestexec{ "ENVNAME": "ENVVALUE" }envenvFiledebuggergdbwirelldboutdebuglineerrrpcshowLogtruemode remote
在 debug 配置中使用 VS Code 变量#
- ${workspaceFolder} 调试 VS Code 打开工作空间的根目录下的所有文件
- ${file} 调试当前文件
- ${fileDirname} 调试当前文件所在目录下的所有文件
使用构建标记(build tags)#
buildFlags -tags=whatever_tag""-tags='first_tag second_tag third_tag'"
debug 配置代码片段
launch.jsonGodebug
调试当前文件配置片段#
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${file}"
}
调试单个测试用例配置片段#
{
"name": "Launch test function",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}",
"args": [
"-test.run",
"MyTestFunction"
]
}
调试包中所有的测试用例配置片段#
{
"name": "Launch test package",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}"
}
调试构建二进制文件配置片段#
{
"name": "Launch executable",
"type": "go",
"request": "launch",
"mode": "exec",
"program": "absolute-path-to-the-executable"
}
远程调试
Delve
$ dlv debug --headless --listen=:2345 --log --api-version=2
Delve
dlv debug --headless --listen=:2345 --log -- -myArg=123
launch.json
{
"name": "Launch remote",
"type": "go",
"request": "launch",
"mode": "remote",
"remotePath": "服务器调试文件的目录(绝对路径)",
"port": 2345,
"host": "127.0.0.1",
"program": "本地调试文件所在的目录(绝对路径)",
"env": {}
}
hostportDelveremotePathprogram remotePath
VS Codedlvdlv
参考链接