Debugging Go with VS Code and Delve

By

Published · Updated

Tagged: Go

Debug Go in VS Code with Delve: install it, configure launch.json with args and env vars, debug a single test, use conditional breakpoints and logpoints, and fix common problems.

VS Code from Microsoft is my favorite code editor. I used tons of editors in the last 20 years and this is the best by far.

It’s Open Source, fast, even on my not-so-recent MacBook Pro, it never crashed in the middle of something, it’s hackable and yet works great out of the box.

It’s all amazing, but how do you debug Go programs? You might think you need a heavy IDE for that, but that’s not true. It’s super easy, with Delve, made by Derek Parker.

Install the Go extension and Delve

First install the official Go extension for VS Code (golang.go, from the Go Team at Google), and make sure Go is set up for the extension.

Then install Delve. On any platform you can open the Command Palette, run Go: Install/Update Tools and pick dlv. On a Mac you can also use Homebrew:

brew install delve

(As I update this post, in November 2026, Homebrew ships Delve 1.27.2.) Or install it with the Go toolchain:

go install github.com/go-delve/delve/cmd/dlv@latest

Check that it works with dlv version.

To keep Delve up to date along with the other Go tools, set "go.toolsManagement.autoUpdate": true in your VS Code settings.

Extra steps on macOS

On macOS, Delve uses the debugger that comes with the Xcode command line tools. Install them if you don’t have them:

xcode-select --install

Unless macOS Developer Mode is enabled, you’re asked to authorize the debugger every time you start it. When that prompt can’t show up, for example when you start dlv from a script, the debug session just hangs. Enable Developer Mode once to stop the prompts:

sudo /usr/sbin/DevToolsSecurity -enable

The Delve docs also say you may need to add your user to the _developer group:

sudo dscl . append /Groups/_developer GroupMembership $(whoami)

Start debugging

In VS Code, open the Run menu and click Start Debugging, or just press F5.

VS Code puts the debug configuration in the .vscode/launch.json file in the root of your workspace. The Go extension generates it for you, and the default it creates is enough for local debugging:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${fileDirname}"
        }
    ]
}

Behind the scenes the extension starts Delve in DAP mode (dlv dap), the Debug Adapter Protocol VS Code speaks natively. You don’t have to start dlv yourself or pick a port. If you find an old launch.json around with "port": 2345, "host" and "remotePath" entries, those belong to the legacy adapter and you can delete them.

The mode parameter can be set to:

Go Debugging with VS Code and Delve

You can see that this is a pretty standard debugger, with local and global variables inspection, the option to watch specific variables, the call stack visualization and breakpoints.

What I especially like about debugging Go, and stepping into lib calls is that the standard library is very clean, well documented and readable, and by debugging you learn a lot of things on how things work internally as well.

Pass arguments and environment variables

Your program probably needs command line arguments, environment variables or a specific working directory. Add them to the configuration:

{
    "name": "Launch server",
    "type": "go",
    "request": "launch",
    "mode": "debug",
    "program": "${workspaceFolder}/cmd/server",
    "args": ["--port", "8080", "--verbose"],
    "env": { "APP_ENV": "development" },
    "envFile": "${workspaceFolder}/.env",
    "cwd": "${workspaceFolder}",
    "buildFlags": ["-tags=dev"]
}

If your program reads from standard input, add "console": "integratedTerminal". The default Debug Console can’t send input to your program.

You can keep several configurations in the configurations array and pick one from the dropdown in the Run and Debug panel.

Debug a single test

Open a _test.go file. Above each test function the Go extension shows a debug test link. Click it to run only that test under the debugger, and it stops at your breakpoints.

That link doesn’t use launch.json. It uses the go.delveConfig and go.testFlags settings. If you need custom settings for a test, write a configuration with "mode": "test" and pass the test name in args:

{
    "name": "Debug TestTotal",
    "type": "go",
    "request": "launch",
    "mode": "test",
    "program": "${workspaceFolder}",
    "args": ["-test.run", "^TestTotal$"]
}

-test.run takes a regular expression, the same as go test -run. Without ^ and $, TestTotal would also match TestTotalWithTax.

Conditional breakpoints and logpoints

Right-click the gutter next to a line number to get more than a plain breakpoint:

In the Breakpoints panel you can also add a function breakpoint by name, like main.total, without opening the file.

Debug a binary you built yourself

With "mode": "exec", Delve runs a binary you compiled. Build it with optimizations and inlining turned off, because they get in the way of debugging:

go build -gcflags=all="-N -l" -o mybin .

Don’t use -ldflags="-s -w" for a binary you want to debug. Those flags strip the debug information, and Delve fails with errors like could not open debug info.

Remote debugging

Remote debugging is not a launch mode anymore: you start a headless Delve server on the target machine (dlv debug --headless --listen=:2345) and connect to it with a configuration that has "request": "attach" and "mode": "remote".

If the code was built in a different folder than the one you have open (a container, another machine), add substitutePath so VS Code can map the remote file paths to your local files:

"substitutePath": [
    { "from": "${workspaceFolder}", "to": "/app" }
]

from is the local path, to is the path on the remote machine. Refer to the official documentation on debugging for the full list of options.

When something doesn’t work

More fixes are in the debugging troubleshooting notes.

Want me to talk about your product? You can sponsor this site.

Related posts about go: