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

huangapple go评论126阅读模式
英文:

How to use Delve debugger in Visual Studio Code

问题

我已经安装了VS Code的Go扩展,但无法使其工作。

“dlv debug”在终端中可以正常工作。

launch.json文件内容如下:

{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}",
"env": {},
"args": []
}
]
}

你知道如何设置吗?

英文:

I have installed the Go extension for VS Code, but unable to make it work.

"dlv debug" works alright from the terminal.

dlv debug src/github.com/user/hello

The launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${workspaceRoot}",
            "env": {},
            "args": []
        }
    ]
}

Do you know how to set it up?

答案1

得分: 72

使用Visual Studio Code和Golang的Delve调试器,按照以下步骤进行操作:

(注意:对于Windows操作系统,请将所有的$GOPATH替换为%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,然后按回车键
  • 点击安装Visual Studio Code的丰富Go语言支持
  • 点击启用并重新启动Visual Studio Code
  • 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设置或切换断点
  • 按下F5开始调试或运行应用程序,如果要选择环境,请选择Go
  • 按下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
        }
    ]
}

结果:

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

英文:

For using Delve debugger in Visual Studio Code with Golang, do the following steps:

( Note: for Windows OS replace all $GOPATH with %GOPATH% )
  • Install Latest Golang and set GOROOT and GOPATH
  • Add $GOPATH/bin to your OS PATH environment variable.
  • set environment variable: GO15VENDOREXPERIMENT = 1
  • run: go get github.com/derekparker/delve/cmd/dlv and make sure dlv binary generated in your $GOPATH/bin
  • Install Visual Studio Code
  • Launch VS Code Quick Open (<kbd>Ctrl</kbd>+<kbd>P</kbd>), paste this command: ext install Go , and press enter.
  • click install Rich Go language support for Visual Studio Code
  • click Enable and restart Visual Studio Code
  • Inside Visual Studio Code Open Folder <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>E</kbd> , e.g.: $GOPATH\src\hello\
  • Then Open hello.go from that folder (or make new file <kbd>Ctrl</kbd>+<kbd>N</kbd> and save it on this folder):

<!-- language: lang-golang -->

package main

import &quot;fmt&quot;

func main() {
	fmt.Println(&quot;Hello World!&quot;)
	i := 101
	fmt.Println(i)
}
  • Then Open Debugger <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>D</kbd>
  • on this line: i := 101 press <kbd>F9</kbd> to set or toggle beakpoint.
  • Press <kbd>F5</kbd> to start debugging or to Run the application, if asked to select environment: select Go.
  • Press <kbd>F10</kbd> to Step Over.
  • Press <kbd>F11</kbd> to Step Into.
  • Press <kbd>Shift</kbd>+<kbd>F11</kbd> to Step Out.
  • Press <kbd>Shift</kbd>+<kbd>F5</kbd> to Stop Debugging.
  • Press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>F5</kbd> to Restart Debugging.

My launch.json untouched:

{
    &quot;version&quot;: &quot;0.2.0&quot;,
    &quot;configurations&quot;: [
        {
            &quot;name&quot;: &quot;Launch&quot;,
            &quot;type&quot;: &quot;go&quot;,
            &quot;request&quot;: &quot;launch&quot;,
            &quot;mode&quot;: &quot;debug&quot;,
            &quot;remotePath&quot;: &quot;&quot;,
            &quot;port&quot;: 2345,
            &quot;host&quot;: &quot;127.0.0.1&quot;,
            &quot;program&quot;: &quot;${workspaceRoot}&quot;,
            &quot;env&quot;: {},
            &quot;args&quot;: [],
            &quot;showLog&quot;: true
        }
    ]
}

Result:

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

答案2

得分: 9

你需要在这里做三件事情:

  • 安装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 Code中使用Delve进行调试了。

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

英文:

You have to do three things here:

  • Install Delve. Looking at your question it seems that you have already installed Delve.
  • Install Go extension for VS Code. The extension can be found at : https://github.com/golang/vscode-go
  • Install dlv tool for Go. You can do that by opening up Command Palette (Ctrl+Shift+P / Cmd+Shift+P) and select Go: Install/Update Tools then search/select dlv

Now you can start debugging with delve in VS code.

More more detailed instruction please follow : https://dev.to/nyxtom/debugging-in-go-in-vs-code-1c7f

答案3

得分: 4

这是我在VSCode中运行Golang调试器时使用的launch.json配置:

{
  "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变量参考:
1: https://code.visualstudio.com/docs/editor/variables-reference.

如果文件/home/your-username/your-project/folder/main.go在VSCode中打开,并且

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

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

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


我的具体值:

$GOROOT: /usr/local/go

$GOPATH: /Users/myname/code

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

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

英文:

This launch.json worked for me to run the Golang debugger in VSCode:

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

VSCode Variables Reference:
1: https://code.visualstudio.com/docs/editor/variables-reference.

If file /home/your-username/your-project/folder/main.go is open in VSCode and

directory /home/your-username/your-project is your root workspace, then

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

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


My specific values:

$GOROOT: /usr/local/go

$GOPATH: /Users/myname/code

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

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

答案4

得分: 0

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

解决方案也在那里发布了:

> ... 在 launch.json 文件的 env 属性中将 GOPATH 添加为环境变量可以解决这个问题。

英文:

FTA (in case it is hard to find), if when using delve and you get cannot find package error even though your GOPATH is set correctly, check out this bug of vscode-go, it is affecting both MAC OS and Linux, as of October, 2017.

The solution is posted there as well:

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

答案5

得分: 0

{
"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
}
]
}
}
]
}

英文:

Content launch.json for gdb and delve

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

}

答案6

得分: 0

如果你遇到错误:无法继续执行:"找不到Delve调试器。从https://github.com/go-delve/delve安装并确保它在你的Go工具路径("GOPATH/bin"或"PATH")中。"

也许你可以尝试我的解决方案(在Mac上):

  1. 首先按照这里的说明安装Delve:https://github.com/go-delve/delve/tree/master/Documentation/installation
  2. 打开你的VSCode,进入你的项目目录,在VSCode终端中运行:go mod init example.com/m/v1
  3. 运行:go get github.com/go-delve/delve/cmd/dlv
  4. 打开Mac终端并运行:open ~/.zshrc(如果不存在,运行:touch ~/.zshrc)
  5. 确保文件中包含以下脚本:

export GOPATH=$HOME/go

export PATH=$PATH:$GOPATH/bin

export PATH=$PATH:$GOROOT/bin

export GOROOT=/usr/local/go

  1. 然后复制.zshrc中的所有脚本
  2. 仍然在Mac终端中,运行:open ~/.bashrc(如果不存在,运行:touch ~/.bashrc)
  3. 将脚本粘贴到.bashrc文件中
  4. 然后保存.zshrc和.bashrc文件
  5. 关闭终端和VSCode
  6. 再次打开VSCode
  7. 最后一步,确保你已经创建了launch.json文件(在.vscode文件夹中),然后你可以按照我的launch.json文件进行操作:
{
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}",
            "args": []
        }
    ]
} 
  1. 完成!你可以再次尝试运行你的程序,并按照接受的答案中的步骤2使用Delve调试器。
英文:

If you got error : Failed to continue: "Cannot find Delve debugger. Install from https://github.com/go-delve/delve & ensure it is in your Go tools path, "GOPATH/bin" or "PATH"."

Maybe you can use my solution (on mac) :

  1. First follow how to install delve on here : https://github.com/go-delve/delve/tree/master/Documentation/installation
  2. Open your vscode, go to your project directory, then on vscode terminal : go mod init example.com/m/v1
  3. run : go get github.com/go-delve/delve/cmd/dlv
  4. Open mac terminal and run : open ~/.zshrc (if not exist, run : touch ~/.zshrc)
  5. Make sure that file contains this script :

export GOPATH=$HOME/go

export PATH=$PATH:$GOPATH/bin

export PATH=$PATH:$GOROOT/bin

export GOROOT=/usr/local/go

  1. Then copy all script on .zshrc
  2. Still on mac terminal, run : open ~/.bashrc (if not exist, run : touch ~/.bashrc)
  3. Paste the script to .bashrc file
  4. Then save .zshrc and .bashrc
  5. close terminal & close vscode
  6. Open vscode again
  7. The last thing, make sure you already create launch.json (in .vscode folder), then you can follow my launch.json :
{
    &quot;version&quot;: &quot;0.2.0&quot;,
    &quot;configurations&quot;: [

        {
            &quot;name&quot;: &quot;Launch&quot;,
            &quot;type&quot;: &quot;go&quot;,
            &quot;request&quot;: &quot;launch&quot;,
            &quot;mode&quot;: &quot;auto&quot;,
            &quot;program&quot;: &quot;${workspaceFolder}&quot;,
            &quot;args&quot;: []
        }
    ]
} 
  1. Then finish ! You can try again run your program and follow step2 using delve on accepted answer

huangapple
  • 本文由 发表于 2016年8月21日 05:22:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/39058823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定