Debugging in Go with VS Code: A Practical Guide**
Debugging is a vital part of every software development process. If you’re using Go (Golang) and Visual Studio Code (VS Code), you have a powerful and smooth environment for debugging your applications. This article walks you through setting up and using the VS Code debugger for Go.
Prerequisites
Before you start debugging, make sure you have the following:
Go installed (download here) – v1.16 or later recommended
Visual Studio Code installed
Go extension for VS Code installed
A basic Go project with a main.go file
Step 1: Install Delve Debugger
VS Code uses the Delve debugger for Go. The Go extension will usually prompt you to install it when needed.
To install manually:
go install github.com/go-delve/delve/cmd/dlv@latest
Ensure that $GOPATH/bin or $HOME/go/bin is included in your system PATH.
Step 2: Create a Go Program
Here’s a basic main.go you can use for testing:
package main
import "fmt"
func main() {
message := "Hello, Debugging World!"
fmt.Println(message)
sum := add(5, 3)
fmt.Println("Sum is:", sum)
}
func add(a, b int) int {
return a + b
}
Step 3: Set Breakpoints
Open main.go in VS Code
Click to the left of the line numbers to set breakpoints, for example on the fmt.Println(message) line
Step 4: Launch the Debugger
Open the Run and Debug panel (Ctrl+Shift+D or use the sidebar icon)
Click “Run and Debug”
Choose “Go” from the list if prompted
VS Code will generate a .vscode/launch.json configuration file automatically
Step 5: Explore Debugging Features
While debugging, you can:
Watch variable values in the VARIABLES panel
Step over (F10) to execute code line by line
Step into (F11) to enter function calls
Hover over variables to inspect their current values
Use the Debug Console to run Go expressions at runtime
Tips
Use log.Println() statements alongside breakpoints for more context
If using Go modules with multiple directories, set the correct cwd in launch.json
Clean builds help avoid using outdated binaries
Some Common Issues
Process exiting with code: 1
Check for build or runtime errors in the code
Breakpoints are not being hit
Make sure you’re debugging the correct executable
Permission denied on macOS
Run sudo DevToolsSecurity -enable and check code signing
Let’s Conclude
Debugging Go with VS Code is efficient and straightforward. With Delve and the Go extension, you can inspect your code’s behavior step-by-step, fix bugs faster, and better understand your applications.