Sunday, June 27, 2021

Intro to Debugging in Go on VSCode

Go-Debugging-Vscode

I wanted to debug a handler function in a Go webserver. To do that first I needed to install delve.

I didn’t have the latest version of delve, so I need to update it.

Next, I created launch.json inside .vscode directory on the root of the repository. The simple way to populate launch.json was to just type go inside “configurations” key and let VSCode auto-populate the config.

go-debugging-vscode

The complete config for launch.json is below:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch file",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${file}",
            "env": {
                "CLIENT_ID":"replaceme",
                "CLIENT_SECRET":"replaceme"
            }
        }
    ]
}

To start the debugging, select (or keep the cursor) the main.go file and press F5 or Run -> Start Debugging. Then I called the endpoint that I wanted to debug. Of course, added breakpoints, stepped in etc.

I also learnt that I can get launch delve directly on the terminal by invoking dlv debug

visi@visis-MacBook-Pro spinup % dlv debug
Type 'help' for list of commands.
(dlv) 

References:

  1. https://ift.tt/2UAO2mm
  2. https://ift.tt/2Wb8p7b
  3. https://ift.tt/3xXQPEx


from Hacker News https://ift.tt/3jjEDdf

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.