如何在Visual Studio中使用Delve调试器代码
问题描述:
我已经为VS代码安装了Go扩展,但无法使其工作。如何在Visual Studio中使用Delve调试器代码
“dlv debug”从终端正常工作。
dlv debug src/github.com/user/hello
的launch.json
:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}",
"env": {},
"args": []
}
]
}
你知道如何设置?
答
对于使用藏坑调试器在Visual Studio代码Golang,请执行下列操作步骤:
(Note: for Windows OS replace all $GOPATH with %GOPATH%)
- 安装最新的Golang,并设置
GOROOT
和GOPATH
- 添加
$GOPATH/bin
您的操作系统PATH
环境变量。 - 设置环境变量:
GO15VENDOREXPERIMENT = 1
- 运行:
go get github.com/derekparker/delve/cmd/dlv
,并在您的$GOPATH/bin
- 产生一定
dlv
二进制安装Visual Studio Code - 启动VS代码快速打开(按Ctrl + P),粘贴此命令:
ext install Go
,然后按回车。 - 点击安装
Rich Go language support for Visual Studio Code
- 点击
Enable
并重新启动Visual Studio代码 - 内
Visual Studio Code
打开文件夹按Ctrl + 移 + ē,如:$GOPATH\src\hello\
- 然后打开
hello.go
从该文件夹(或作新文件Ctrl + N并将其保存在此文件夹中):
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
i := 101
fmt.Println(i)
}
- 然后打开调试器Ctrl键 + 移 + d
- 在这条线:
i := 101
按F9设置或切换beakpoint。 - 按F5开始调试或运行应用程序,如果要求选择环境:请选择
Go
。 - 按F10来跳过。
- 按F11步入。
- Press Shift + F11 to Step Out。
- Press Shift + F5停止调试。
- 按下Ctrl键+移+ F5 到重新启动调试。
我launch.json
不变:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceRoot}",
"env": {},
"args": [],
"showLog": true
}
]
}
结果:
答
FTA(如果它是很难找到),如果使用delve
,当你得到cannot find package
错误,即使您的GOPATH
设置正确,检查出this bug of vscode-go,它影响到MAC OS和Linux,截至2017年10月。
的解决方案张贴有作为:
... adding the GOPATH as an env var in the env property in the launch.json file solved the problem
@克里斯-G \t我希望这有助于。 – 2016-08-21 10:09:09
如果你和VS Code有一些联系,试试: '文件/关闭文件夹', '文件/打开文件夹', '在资源管理器的左侧面板上点击'hello.go'并打开它' , '按F9为突破point', '按F5键选择进入',' JSON关闭file', '点击debugger', '按F5' – 2016-08-23 12:17:35
谢谢!尽管我仍然无法让调试器工作。我得到:无法加载软件包:软件包。:无法建立Go源文件在/ Users/xx/godeep 退出状态1 –