Debug Golang with VSCode

2023-05-01

Debug Golang with VSCode

Example

Create a simple go lang file with the below code:

  • main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main

import (
"fmt"
"os"
"strings"
)

func main() {
fmt.Println("hello world")

name := os.Args[1:]

fmt.Println("hello " + strings.Join(name, ""))
}

Make sure the go.mod is initialized

  • go.mod
1
2
3
module test-debug

go 1.19

it will print the args from the command-line args

Switch to the Run and Debug

And choose the create a launch.json file and fill in the config for debugging

The config example is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
// 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 Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"args": ["nick","ben"]
}
]
}

the args are the arguments you want to put after the go program, for example:

go run main.go nick ben

then the “args“ is:

"args": ["nick","ben"]

Now you can start your debugging