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

时间:2016-08-20 21:22:40

标签: go visual-studio-code delve

我已经为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": []
        }
    ]
}

你知道如何设置吗?

5 个答案:

答案 0 :(得分:55)

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

( Note: for Windows OS replace all $GOPATH with %GOPATH% )
  • 安装最新的Golang并设置GOROOTGOPATH
  • $GOPATH/bin添加到您的操作系统PATH环境变量中。
  • 设置环境变量:GO15VENDOREXPERIMENT = 1
  • 运行:go get github.com/derekparker/delve/cmd/dlv并确保在dlv
  • 中生成$GOPATH/bin二进制文件
  • 安装Visual Studio Code
  • 启动VS Code快速打开( Ctrl + P ),粘贴此命令:ext install Go,然后按Enter键。
  • 点击安装Rich Go language support for Visual Studio Code
  • 单击Enable并重新启动Visual Studio代码
  • 内部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 := 101 F9 设置或切换beakpoint。
  • F5 开始调试或运行应用程序,如果要求选择环境:选择Go
  • F10 跳过。
  • F11 进入Step Into。
  • 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
        }
    ]
}

结果:

enter image description here

答案 1 :(得分:2)

你必须在这里做三件事:

  • 安装 Delve。查看您的问题,您似乎已经安装了 Delve。
  • 为 VS Code 安装 Go 扩展。可以在以下位置找到扩展名:https://github.com/golang/vscode-go
  • 为 Go 安装 dlv 工具。您可以通过打开命令面板 (Ctrl+Shift+P / Cmd+Shift+P) 并选择 Go: Install/Update Tools 然后搜索/选择 dlv

现在您可以在 VS 代码中使用 delve 开始调试了。

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

答案 2 :(得分:1)

这个%matplotlib inline import matplotlib.pyplot as plt import pandas as pd data = {'A': {'pos': 289794, 'neg': 515063}, 'B': {'pos': 174790, 'neg': 292551}, 'C': {'pos': 375574, 'neg': 586616}, 'D': {'pos': 14932, 'neg': 8661}} df = pd.DataFrame(data) df = df.T df ['sum'] = df.sum(axis=1) df.sort_values('sum', ascending=False)[['neg','pos']].plot.bar() 对我来说可以在VSCode中运行Golang调试器:

launch.json

VSCode Variables Reference: 如果文件{ "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中打开,并且

目录/home/your-username/your-project/folder/main.go是您的根工作区,然后

$ {file} = /home/your-username/your-project

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


我的具体值:

$ GOROOT:/home/your-username/your-project/folder

$ GOPATH:/usr/local/go

$ {file}:/Users/myname/code

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

答案 3 :(得分:0)

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

解决方案也在那里发布:

  

...在launch.json文件的env属性中添加GOPATH作为env var解决了问题

答案 4 :(得分:0)

内容launch.json for gdb and delve

{
// Используйте 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
                }
            ]
        },
    }
]

}