Debug Go in VS Code
2 minutes read | 249 words by Ruben BerenguelSo, you want to debug some Go in VS Code. And maybe setup some Docker containers with services before? How the hell do you configure that?

Imagine that you are working on a large Go project, with pkg
, internal
, cmd
and all the folders, and want to debug while running against several local containers, like databases, or Kafka.
The documentation in the VS Code docs is a bit confusing, or excessively long. You need two files in your .vscode
. The first is launch.json
, if you don’t have one as soon as you trigger the debugger it will prompt you to create one.
{
"version": "0.2.0",
"configurations": [{
"name": "Debug",
"preLaunchTask": "docker-init",
"postDebugTask": "docker-terminate",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}/cmd/main-thingy",
"cwd": "${workspaceFolder}",
"args": [
"--headless", "etcetera"
],
"env": { "VARIABLE": "VALUE" }
"envFile": "${workspaceFolder}/.env"
}]
}
You may not need all the settings, but I left them in case they are helpful.
The setup above is required for launching a debugger, and preLaunchTask
is the label of a task defined in task.json
, also in your .vscode
folder. It will run before starting the debugger. And postDebugTask
will run once you stop the debugger. Like a setup
and teardown
in a test suite.
{
"version": "2.0.0",
"tasks": [
{
"label": "docker-init",
"type": "shell",
"command": "docker compose up -d",
},
{
"label": "docker-terminate",
"type": "shell",
"command": "docker compose down",
}
]
}
Of course your setup may vary, but I hope you can copy paste this and run with it.