How to use Delve debugger in Visual Studio Code
Solution 1
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:
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
Solution 2
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
Chris G.
Updated on August 05, 2021Comments
-
Chris G. almost 3 years
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?