To debug your Golang application using Delve in Visual Studio Code, follow these steps:
-
Install Delve:
go install github.com/go-delve/delve/cmd/dlv@latest
-
Ensure Go Path is Set in Your Shell: Add the following lines to your shell configuration file (e.g.,
.zshrc
or.bashrc
):. ~/.asdf/plugins/golang/set-env.zsh export GOPATH=$HOME/.asdf/installs/golang/1.22.10 export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Then, reload your shell configuration:
source ~/.zshrc # Use ~/.bashrc for Bash
-
Add Delve Path to Visual Studio Code Settings: Open Visual Studio Code settings and update your
settings.json
file with the following configuration:{ "go.alternateTools": { "dlv": "/home/martins/.asdf/installs/golang/1.22.10/bin/dlv" } }
-
Create Debug Configuration: Create or update the
.vscode/launch.json
file in your project directory with the following configuration:{ "version": "0.2.0", "configurations": [ { "name": "golang debug", "type": "go", "request": "launch", "mode": "auto", "program": "${workspaceFolder}/cmd/server", // Path to application entry point "env": { "APP_PORT": "#", "DB_HOST": "#", "DB_USER": "#", "DB_PASSWORD": "#", "DB_NAME": "#", "DB_PORT": "#", "JWT_SECRET": "#", "JWT_EXPIRES_IN": "#" }, "args": [], "cwd": "${workspaceFolder}" } ] }
With these steps completed, you can press F5
in Visual Studio Code to start debugging your application.