3 Easy steps to interactively debug Go code with Visual Studio Code in Windows

If you are like me, then you like to be able to write code and develop both from the same integrated environment. Microsoft’s Visual Studio is my favorite IDE. Not long ago, Microsoft released a new free editor called VS Code. It is a powerful editor that is highly configurable and customizable (with extensions).

In this blog post, I am going to illustrate how to set up VS Code in order to debug source code written in the Go language.

Step 1 – Installing the Go language support

First, install the Go language on your computer from here: https://golang.org/dl/

If you did not install VS Code before, install it from here: https://code.visualstudio.com/Download

From inside VS Code, install the Go language extension by clicking on the extensions icon and then searching for “Go” and installing it.

Press the “Reload” button once the extension is installed.

Step 2 – Installing Delve (Go debugger)

Delve is a Go language debugger. On Windows, you can follow the installation instructions from here: https://github.com/derekparker/delve/blob/master/Documentation/installation/windows/install.md

Basically, the installation step instructs Go to download the package:

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

If everything goes fine, then the debugger (dlv.exe) should have been installed here: %USERPROFILE%\go\bin\dlv.exe

Step 3 – Configuring VS Code with the Delve debugger

In this last step, we need to create a custom VS Code launch.json to tell the VS Code how to launch and debug our Go code.

First, in VS Code, use “Open folder” command and select the folder where your *.go exist:

Afterwards, click on the debugger icon and then on the drop down (next to the DEBUG menu) and choose “Add configuration”:

If everything is working correctly, a menu will appear with various Go language related entries. Select the “Go: Launch file” option:

Edit the configuration to look like the following:

{
    "version": "0.2.0",
    "configurations": [
    {
        "name": "Launch file",
        "type": "go",
        "request": "launch",
        "mode": "debug",
        "program": "${file}"
    },
    ]
}

That’s it. You are now ready to put breakpoints (F9) and start stepping into your code (F10, F11). Hit F5 on your keyboard to run your program under the debugger:

Finally, as you noticed, there’s no way to pass command line arguments (at least not with the launch configuration we added previously). To remedy this, we can create another launch configuration with the “args” option like so:

    "configurations": [
    {
        "name": "Launch file",
        "type": "go",
        "request": "launch",
        "mode": "debug",
        "program": "${file}",
        "args":[
            "arg1", "arg2", "arg3"
        ]
    },
Note that the “args” option is list, therefore each command line argument that you want to pass should be represented as its own array element.
For example if you want to run the script as: myscript.go --filename hello.txtthen your args array should look like:
        "args":["--filename", "hello.txt"]

That’s it!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.