How to Debug Golang with VS Code
Summary
- Basic
- Spec
- Instruction
- debugging unit test
- debugging executable file
- debugging local process
- debugging running remote process
Basic
- The Go Programming Language
- Extension: Go
- Debugger: delve
- module code: bubbleSort.go
Spec
- OS
- ✅ MacOS
- ✅ Windows
- ✅ Linux
- Break Point
- ✅ break point
- ✅ condition break point
- ❌ function breakpoint
- Step Execution
- ✅ Step Over
- ✅ Step Into
- ✅ Step Out
- ✅ Continue
- Variables
- ✅ variables views
- ✅ watch variables
- Call Stack
- ✅ call stack
- Evaluation
- ✅ eval expression to show variables
- ✅ eval expression to change variables
- Type of Execution
- ✅ debug unit test
- ✅ debug executable package
- ✅ remote debugging
Instruction
MacOS
- install go :
brew install golang
- add go/bin to PATH
- install extension “Go”
- install other tools:
F1
->Go: Install/Update Tools
Windows
- install go and add go/bin to PATH
- install extension “Go”
- install other tools:
F1
->Go: Install/Update Tools
Linux
- install go and add go/bin to PATH
- install extension “Go”
- install other tools:
F1
->Go: Install/Update Tools
unit test
source : bubbleSort_test.go
inline
launch json
menu:Go: Launch test function
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch test function",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}",
"args": [
"-test.run",
// test function name
// * can use reguler expression
// * NOT include "Test"
// * the first charactor MUST be small
"bubblesort"
]
}
]
}
program
must be package folder
using Test Explorer
install Go Test Explorer
ext install ethan-reesor.vscode-go-test-adapter
debugging executable file
source: bubblesorter/cmd/bubblesorter/bubbleSorter.go
launch.json
menu:Go: Launch package
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/bubblesorter/cmd/bubbleSorter"
}
]
}
program
must be main package folder or *.go file
start debugging
▶︎ Launch Package
debugging local process
source: bubblesorter/cmd/bubblesorter/bubbleSorter.go
prepare
# build executable file
cd bubblesorter/cmd/bubblesorter
go build
# enable ptrace scope (for Linux)
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
start process
./bubblesorter -sleep 30 7 4 2 6 &
[1] 1859211
edit launch.json
Add processId to launch.json.
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach local process",
"type": "go",
"request": "attach",
"mode": "local",
"processId": 1859211,
"apiVersion": 2,
"showLog": true
}
]
}
start debugging
▶︎ Attach local process
debugging running remote process
prepare
cd cmd/bubbleSorter/
go build
# enable ptrace scope (for Linux)
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
execute and dlv attach
cd cmd/bubbleSorter/
# runnning process
./bubbleSorter -sleep 30 &
PID=$!
dlv attach $PID ./bubbleSorter --headless --listen=0.0.0.0:2345 --log --api-version 2
edit launch.json
Edit host to remote server address.
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach remote process",
"type": "go",
"request": "attach",
"mode": "remote",
"port": 2345,
"host": "127.0.0.1",
"apiVersion": 2,
"showLog": true
}
]
}
start debugging
▶︎ Attach remote process