2016-08-20 108 views
6

我已經爲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": [] 
     } 
    ] 
} 

你知道如何設置?

回答

24

對於使用藏坑調試器在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 –

0

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

相關問題