本篇文章给大家介绍一下使用VSCode调试Golang工程的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
推荐学习:《vscode教程》
在官网下载最新版的VSCode:
Visual Studio Code - Code Editing. Redefined**
添加目标调试配置
例子:
{ "version": "0.2.0", "configurations": [ { "name": "Launch", "type": "go", "request": "launch", "mode": "debug", "remotePath": "", "port": 2345, "host": "127.0.0.1", "program": "${fileDirname}", "env": { "GOPATH":"D:/Develop/vscodegolang" }, "args": [], "showLog": true } ] }
其中: "port", "host"都是go插件自动生成的
"env"为设置环境变量, 设置为你的工程目录就可以(包含bin, src的文件夹)
此时找到main.go按F5, 会报错提示:
Failded to continue:"Cannot find Delve debugger. Install from https://github.com/derekparker/delve & ensure it is in your "GOPATH/bin" or "PATH"
我们使用go命令行编译调试器
go get github.com/derekparker/delve/cmd/dlv
将dlv调试器放在GOPATH(工程目录)的bin目录下
选中要调试的main.go, 点击F5, 既可以开始调试
调试快捷键和Visual Studio系一致
注意点
在launch.json中可以添加多组调试入口, 通过调试面板中选中对应的配置开启不同目标的调试
{ "version": "0.2.0", "configurations": [ { "name": "client", "type": "go", "request": "launch", "mode": "debug", "remotePath": "", "port": 2345, "host": "127.0.0.1", "program": "${fileDirname}", "env": { "GOPATH":"D:/Develop/vscodegolang" }, "args": [], "showLog": true }, { "name": "server", "type": "go", "request": "launch", "mode": "debug", "remotePath": "", "port": 2345, "host": "127.0.0.1", "program": "${workspaceRoot}/src/server", "env": { "GOPATH":"D:/Develop/vscodegolang" }, "args": [], "showLog": true } ] }
"program"中的"${fileDirname}"是以当前选中文件作为启动点
更建议使用"program"的"${workspaceRoot}", 以包名作为启动点的方式进行配置
更多编程相关知识,请访问:编程视频!!
以上是详解VSCode中如何调试Golang工程的详细内容。更多信息请关注PHP中文网其他相关文章!