Delve is a debugger for the Go programming language. The goal of the project is to provide a simple, full featured debugging tool for Go. Delve should be easy to invoke and easy to use. Chances are if you're using a debugger, things aren't going your way.
If you use VS Code to run your Go code and have the official Go extension installed on the editor, you can debug your code by pressing F5 or Run and Debug on your computer.
The launch. json file is located in a . vscode folder in your workspace (project root folder).
For using Delve debugger in Visual Studio Code with Golang, do the following steps:
( Note: for Windows OS replace all $GOPATH with %GOPATH% )
GOROOT and GOPATH
$GOPATH/bin to your OS PATH environment variable. GO15VENDOREXPERIMENT = 1 go get github.com/derekparker/delve/cmd/dlv and make sure dlv binary generated in your $GOPATH/bin ext install Go , and press enter. Rich Go language support for Visual Studio Code Enable and restart Visual Studio Code Visual Studio Code Open Folder Ctrl+Shift+E , e.g.: $GOPATH\src\hello\
hello.go from that folder (or make new file Ctrl+N and save it on this folder): package main
import "fmt"
func main() {
fmt.Println("Hello World!")
i := 101
fmt.Println(i)
}
i := 101 press F9 to set or toggle beakpoint. Go. 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:

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
You have to do three things here:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With