英文:
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并设置
GOROOT
和GOPATH
- 将
$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
}
]
}
结果:
英文:
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
andGOPATH
- Add
$GOPATH/bin
to your OSPATH
environment variable. - set environment variable:
GO15VENDOREXPERIMENT = 1
- run:
go get github.com/derekparker/delve/cmd/dlv
and make suredlv
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 "fmt"
func main() {
fmt.Println("Hello World!")
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:
{
"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
}
]
}
Result:
答案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 selectGo: Install/Update Tools
then search/selectdlv
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:
{
"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:
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
"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
}
]
},
}
]
}
答案6
得分: 0
如果你遇到错误:无法继续执行:"找不到Delve调试器。从https://github.com/go-delve/delve安装并确保它在你的Go工具路径("GOPATH/bin"或"PATH")中。"
也许你可以尝试我的解决方案(在Mac上):
- 首先按照这里的说明安装Delve:https://github.com/go-delve/delve/tree/master/Documentation/installation
- 打开你的VSCode,进入你的项目目录,在VSCode终端中运行:go mod init example.com/m/v1
- 运行:go get github.com/go-delve/delve/cmd/dlv
- 打开Mac终端并运行:open ~/.zshrc(如果不存在,运行:touch ~/.zshrc)
- 确保文件中包含以下脚本:
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
export GOROOT=/usr/local/go
- 然后复制.zshrc中的所有脚本
- 仍然在Mac终端中,运行:open ~/.bashrc(如果不存在,运行:touch ~/.bashrc)
- 将脚本粘贴到.bashrc文件中
- 然后保存.zshrc和.bashrc文件
- 关闭终端和VSCode
- 再次打开VSCode
- 最后一步,确保你已经创建了launch.json文件(在.vscode文件夹中),然后你可以按照我的launch.json文件进行操作:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": []
}
]
}
- 完成!你可以再次尝试运行你的程序,并按照接受的答案中的步骤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) :
- First follow how to install delve on here : https://github.com/go-delve/delve/tree/master/Documentation/installation
- Open your vscode, go to your project directory, then on vscode terminal : go mod init example.com/m/v1
- run : go get github.com/go-delve/delve/cmd/dlv
- Open mac terminal and run : open ~/.zshrc (if not exist, run : touch ~/.zshrc)
- 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
- Then copy all script on .zshrc
- Still on mac terminal, run : open ~/.bashrc (if not exist, run : touch ~/.bashrc)
- Paste the script to .bashrc file
- Then save .zshrc and .bashrc
- close terminal & close vscode
- Open vscode again
- The last thing, make sure you already create launch.json (in .vscode folder), then you can follow my launch.json :
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": []
}
]
}
- Then finish ! You can try again run your program and follow step2 using delve on accepted answer
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论