vscode調試golang環境搭建及配置

準備VSCode

在官網下載最新版的VSCode:git

安裝Golang插件

  • 打開擴展面板github

    VSCode->查看->擴展golang

  • 找到Go插件 在搜索框裏輸入Go, 找到第二行寫有 Rich Go language support for Visual Studio Code的插件, 點擊安裝json

    注意不是排名最高的編輯器

  • 重啓編輯器spa

配置啓動項

  • 打開調試面板插件

    VSCode->查看->調試命令行

  • 添加調試目標debug

    在"沒有調試"的下拉框中點擊"添加配置.."調試

  • 添加目標調試配置

    例子:

    {
        "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插件自動生成的

"env"爲設置環境變量, 設置爲你的工程目錄就能夠(包含bin, src的文件夾)

準備調試插件

此時找到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

將dlv調試器放在GOPATH(工程目錄)的bin目錄下

開始調試

選中要調試的main.go, 點擊F5, 既能夠開始調試

調試快捷鍵和Visual Studio系一致

  • F9 切換斷點
  • F10 Step over
  • F11 Step in
  • Shift+F11 Step out

注意點

  • 某些結構體成員沒法直接顯示時, 能夠直接選中變量名, 添加到監視, 或者右鍵點擊: "調試:求值"

多項目調試

在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}", 以包名做爲啓動點的方式進行配置

參考連接

Debugging in Visual Studio Code

相關文章
相關標籤/搜索