如何在 Visual Studio 代码中使用 Delve 调试器

How to use Delve debugger in Visual Studio Code

我已经安装了 VS Code 的 Go 扩展,但无法运行。

"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": []
        }
    ]
}

你知道怎么设置吗?

要在 Visual Studio Code with Golang 中使用 Delve 调试器,请执行以下步骤:

( Note: for Windows OS replace all $GOPATH with %GOPATH% )
  • 安装最新的 Golang 并设置 GOROOTGOPATH
  • $GOPATH/bin 添加到您的 OS PATH 环境变量。
  • 设置环境变量:GO15VENDOREXPERIMENT = 1
  • 运行: go get github.com/derekparker/delve/cmd/dlv 并确保 dlv 二进制文件在你的 $GOPATH/bin
  • 中生成
  • 安装Visual Studio Code
  • 启动 VS Code Quick Open (Ctrl+P),粘贴此命令:ext install Go,然后按回车键。
  • 点击安装 Rich Go language support for Visual Studio Code
  • 点击Enable并重启Visual Studio代码
  • Inside Visual Studio Code 打开文件夹 Ctrl+Shift+E ,例如: $GOPATH\src\hello\
  • 然后从该文件夹中打开 hello.go(或新建文件 Ctrl+N 并将其保存在该文件夹中) :
package main

import "fmt"

func main() {
    fmt.Println("Hello World!")
    i := 101
    fmt.Println(i)
}
  • 然后打开调试器Ctrl+Shift+D
  • 在这一行:i := 101F9 设置或切换 beakpoint。
  • F5开始调试或运行应用程序,如果要求select环境:selectGo .
  • F10 跳过。
  • F11 进入。
  • Shift+F11跳出。
  • Shift+F5停止调试。
  • Ctrl+Shift+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
        }
    ]
}

结果:

FTA(如果很难找到),如果在使用 delve 时出现 cannot find package 错误,即使 GOPATH 设置正确,请查看 this bug of vscode-go,截至 2017 年 10 月,它同时影响 MAC OS 和 Linux。

解决方案也发布在那里:

... adding the GOPATH as an env var in the env property in the launch.json file solved the problem

gdb 和 delve 的内容launch.json

{
// Используйте IntelliSense, чтобы узнать о возможных атрибутах.
// Наведите указатель мыши, чтобы просмотреть описания существующих атрибутов.
// Для получения дополнительной информации посетите: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "Delve",
        "type": "go",
        "request": "launch",
        "mode": "debug",
        "remotePath": "",
        "port": 2345,
        "host": "127.0.0.1",
        "program": "${workspaceRoot}/src/hello/hello.go",
        "env": {},
        "args": [],
        "showLog": true
    }
   ,
    {
        "type": "gdb",
        "request": "launch",
        "name": "GDB",

        "target": "${workspaceRoot}/src/hello/hello",
        "cwd": "${workspaceRoot}",
        "linux": {
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
    }
]

}

这个 launch.json 对我有用 运行 VSCode 中的 Golang 调试器:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch file",
      "type": "go",
      "request": "launch",
      "mode": "auto",
      "program": "${file}",
      "env": {
        "PATH": "/usr/local/go/bin:${fileDirname}"
      },
      "args": []
    }
  ]
}

VSCode Variables Reference: 如果文件 /home/your-username/your-project/folder/main.go 在 VSCode 和

中打开

目录/home/your-username/your-project是你的根工作区,然后

${文件} = /home/your-username/your-project/folder/main.go

${fileDirname} = /home/your-username/your-project/folder


我的具体价值观:

$GOROOT: /usr/local/go

$GOPATH: /Users/myname/code

${文件}: /Users/myname/code/src/github.com/githubName/appName/main.go

${文件目录名}: /Users/myname/code/src/github.com/githubName/appName

你必须在这里做三件事:

  • 安装 Delve。看你的问题好像你已经安装了Delve
  • 为 VS 代码安装 Go 扩展。可以在以下位置找到扩展:https://github.com/golang/vscode-go
  • 为 Go 安装 dlv 工具。您可以通过打开命令面板 (Ctrl+Shift+P / Cmd+Shift+P) 和 select Go: Install/Update Tools 然后 search/select dlv
  • 来做到这一点

现在您可以开始使用 VS 代码中的 Delve 进行调试。

更详细的说明请关注:https://dev.to/nyxtom/debugging-in-go-in-vs-code-1c7f