Cover Image

Debugging GoLang tests with Visual Studio Code and Delve

January 10, 2017 - Reading time: 5 minutes

Versions used in this tutorial:

Visual Studio Code 1.8.1

Rich Go language support for Visual Studio Code 0.6.52 by lukehoban

GoLang 1.7.4

Delve 0.11.0-alpha

First make sure that the Go extension is installed, if not press Ctrl+P and enter the following command to install it

ext install Go

Now it's time to install Delve, if you're using OSX follow this guide, on Windows and Linux use go get to install.

go get github.com/derekparker/delve/cmd/dlv

When Delve is installed, press F5 to start the debugger and a search bar is opened.

Choose Go from the list. This will create a launch.json file which contains the settings for the debugger.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}",
            "env": {},
            "args": [],
            "showLog": true
        }
    ]
}

To be able to debug tests we will have to change the "mode" property to "test" and save.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "test",
            "remotePath": "",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}",
            "env": {},
            "args": [],
            "showLog": true
        }
    ]
}

This will enable us to set a breakpoint in our test code and run the debugger with F5. Now the debugger breaks in the test code and we can continue to debug our code.