隨著互聯網時代的飛速發展,我們編碼使用的開發利器也在不斷更新換代,古話說工欲善其事必先利其器。對于Go語言程序的開發者而言,當下最火的IDE應該非微軟的Visual Studio Code莫屬,下面給出怎樣在vscode中調試go程序的過程,以mac本為示例。
當前已經準備好go的待調試代碼,main.go的函數位置為:
/Users/tingfeng/Workspace/go/src/github.com/nsharecome/caccount/main.go
1、編輯launch.json文件
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "debug",
"type": "go",
"request": "launch",
"mode": "debug",
"port": 8899,
"host": "127.0.0.1",
"program": "/Users/tingfeng/Workspace/go/src/github.com/nsharecome/caccount/main.go",
"env": {},
"args": [ ],
"output": "${workspaceRoot}/bin/debug_caccount",
"showLog": true
},
]
}
2、點擊運行,報錯如下:
2021/02/07 16:51:52 /Users/tingfeng/Workspace/go/src/github.com/nsharecome/caccount/main.go/debugger.go:101: launching process with args: [/Users/tingfeng/Workspace/go/src/github.com/nsharecome/caccount/bin/debug_caccount]
could not launch process: decoding dwarf section info at offset 0x0: too short
Process exiting with code: 1
錯誤原因如下:安裝delve
解決方式:
git clone https://github.com/go-delve/delve.git $GOPATH/src/github.com/go-delve/delve
go get -u github.com/derekparker/delve/cmd/dl
利用delve手動調試可以參考:Delve調試器