Debugging Go Code With Visual Studio Code

  1. Open Visual Studio Code

  2. Create your project

  3. Create code you want to test. Will be using the following sample:

    ``package main

    import (
      "fmt"
    )

    func main() {
      ar := area(10, 5)
      per := perimeter(10, 5)

      fmt.Println("Area: ", ar)
      fmt.Println("Perimeter: ", per)
    }

    func perimeter(width, height int) int {
      return 2*width + 2*height
    }

    func area(width, height int) int {
      return width * height
    }``
  1. Install Delve package
    go get github.com/derekparker/delve/cmd/dlv
  1. From the menu, click View > Debug or Ctrl+Shft+D to open the Code debug viewlet

  2. Click on the gear icon to create a launch.json configuration file for the project

alt='Visual Studio Code debug configuration icon'

  1. Contents of the created launch.json file will similar to the following:
      {
        "version": "0.2.0",
        "configurations": [
          {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${fileDirname}",
            "env": {},
            "args": [],
            "showLog": true
          }
        ]
      }

Setup Go Development Environment with Visual Studio Code https://rominirani.com/setup-go-development-environment-with-visual-studio-code-7ea5d643a51a#4b5d

Debugging Go code using VS Code https://github.com/Microsoft/vscode-go/wiki/Debugging-Go-code-using-VS-Code

How to use Delve debugger in Visual Studio Code https://stackoverflow.com/questions/39058823/how-to-use-delve-debugger-in-visual-studio-code

Debugging Go code using VS Code https://github.com/Microsoft/vscode-go/wiki/Debugging-Go-code-using-VS-Code