Go

Go Programming With Visual Studio Code

Published 2026-03-01.
Time to read: 1 minutes.

This page is part of the golang collection.

Debugging Go Tests In Visual Studio Code

To debug Go tests using Visual Studio Code, use the Delve debugger.

Install the latest release of Delve:

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

Create a new launch configuration in .vscode/launch.json that resembles:

.vscode/launch.json fragment
{
  "args": [
    "-test.run", "Test.*",
    "-test.v"
  ],
  "mode": "test",
  "name": "Run core/cmd/sc_router tests",
  "program": "${workspaceFolder}/core/cmd/sc_router",
  "request": "launch",
  "type": "go",
},

Comments on the above:

  • The names of dlv command-line arguments such as -run and -v must be prefaced with test. as shown in launch.json.
  • The program argument is the directory path (not the file name) containing the tests. All source files in that package will be examined, including the _test.go files, so the -test.run option can filter for the function(s) you want to test.
  • If you set program to a specific file path instead of a directory path, the Go test runner often fails to resolve the package context correctly because Go tests are compiled and executed at the package level, not the file level.
  • The names of the tests to run is specified by the argument for -test.run, which is a regex. The regex Test.* runs all tests whose names start with Test.

Press CTRL+Shift+D to open the Run and Debug view in the sidebar.

Select the new configuration (e.g., Run core/cmd/sc_router tests) from the dropdown menu at the top of the sidebar.

Set breakpoints in the Go code for the test that you want to debug.

Click the green play button or press F5 to start debugging.

Delve Help Message

The help message for Delve is:

Output of: dlv -h
Delve is a source level debugger for Go programs.

Delve enables you to interact with your program by controlling the execution of the process,
evaluating variables, and providing information of thread / goroutine state, CPU register
state and more.

The goal of this tool is to provide a simple yet powerful interface for debugging Go programs.

Pass flags to the program you are debugging using `--`, for example:

`dlv exec ./hello -- server --config conf/config.toml`

Usage:
  dlv [command]

Available Commands:
  attach                       Attach to running process and begin debugging.
  completion                   Generate the autocompletion script for the specified shell
  connect                      Connect to a headless debug server with a terminal client.
  core                         Examine a core dump.
  dap                          Starts a headless TCP server communicating via Debug Adaptor Protocol (DAP).
  debug                        Compile and begin debugging main package in current directory, or the package specified.
  exec                         Execute a precompiled binary, and begin a debug session.
  help                         Help about any command
  test                         Compile test binary and begin debugging program.
  trace                        Compile and begin tracing program.
  version                      Prints version.

Additional help topcis:
  dlv backend                      Help about the --backend flag.
  dlv log                          Help about logging flags.
  dlv redirect                     Help about file redirection.

Use "dlv [command] --help" for more information about a command.
* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.