How to debug your Go application in VSCode

huihzhao

Hui`s Journal of Technology

Posted on March 23, 2022

How to debug your Go application in VSCode

I am using VScode as my primary editor of coding, and I feel very excited by the VScode ecosystem. However, when I first used it to debug go application, as the debug settings is not that straightforward, I spent one hour to learn how to setup the debug process with VScode.

Before you start, it'd better to install the dlv, the go debugger, manually.

brew install dlv
Enter fullscreen mode Exit fullscreen mode

Validate your dlv installation.

dlv version
Delve Debugger
Version: 1.8.2
Build: $Id: dbb493ec14d1e7753504d016b1e1ef1665b75b16 
Enter fullscreen mode Exit fullscreen mode

Then open VSCode in your root directory of your go project. Usually, in the root directory, you can find your main.go, go.mod, and go.sum files.

Open your debug settings, which is a launch.json file saved in your code directory of ".vscode".

Add the following contents:

    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch file",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "main.go",
            "args": ["arg1", "arg2"]
        }

    ]
}
Enter fullscreen mode Exit fullscreen mode

Then you can start your debugging by press "F5" now.

💖 💪 🙅 🚩
huihzhao
Hui`s Journal of Technology

Posted on March 23, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related