如何在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,并设置GOROOTGOPATH
  • 添加$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 := 101F9设置或切换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 
     } 
    ] 
} 

结果:

enter image description here

+0

@克里斯-G \t我希望这有助于。 – 2016-08-21 10:09:09

+0

如果你和VS Code有一些联系,试试: '文件/关闭文件夹', '文件/打开文件夹', '在资源管理器的左侧面板上点击'hello.go'并打开它' , '按F9为突破point', '按F5键选择进入',' JSON关闭file', '点击debugger', '按F5' – 2016-08-23 12:17:35

+1

谢谢!尽管我仍然无法让调试器工作。我得到:无法加载软件包:软件包。:无法建立Go源文件在/ Users/xx/godeep 退出状态1 –

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