隨著互聯(lián)網(wǎng)時(shí)代的飛速發(fā)展,我們編碼使用的開發(fā)利器也在不斷更新?lián)Q代,古話說工欲善其事必先利其器。對(duì)于Go語言程序的開發(fā)者而言,當(dāng)下最火的IDE應(yīng)該非微軟的Visual Studio Code莫屬,下面給出怎樣在vscode中調(diào)試go程序的過程,以mac本為示例。
當(dāng)前已經(jīng)準(zhǔn)備好go的待調(diào)試代碼,main.go的函數(shù)位置為:
/Users/tingfeng/Workspace/go/src/github.com/nsharecome/caccount/main.go
1、編輯launch.json文件
{
// 使用 IntelliSense 了解相關(guān)屬性。
// 懸停以查看現(xiàn)有屬性的描述。
// 欲了解更多信息,請(qǐng)?jiān)L問: 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、點(diǎn)擊運(yùn)行,報(bào)錯(cuò)如下:
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
錯(cuò)誤原因如下:安裝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手動(dòng)調(diào)試可以參考:Delve調(diào)試器