本篇文章跟大家介紹一下使用VSCode來偵錯Golang工程的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有幫助。
推薦學習:《vscode教學》
準備VSCode
在官網下載最新版的VSCode:
範例:
{ "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外掛自動產生的
準備調試插件
此時找到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
開始調試
選中要調試的main.go, 點擊F5, 既可以開始調試
多重專案偵錯
在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中文網其他相關文章!