2
0

More than 3 years have passed since last update.

Simple Go debug with VSCode

Last updated at Posted at 2021-02-16

I'm writing this article out of frustration after trying without success debugging Go on VSCode with AWS SAM Lambda local. I then changed to a simpler Go debug and it's enough development at the moment. This proves that KISS works without a doubt 😅.

Background

What was supposed to be a simple task of setting step debugger for AWS SAM with Go programming language turned out to be a harrowing tale of woe and helplessness. Yes, it's not that easy to set up debugging for Go as opposed to Node.Js which is smooth sailing.

How to debug

Prerequisites
I supposed you already have Go installed and set up (with module for latest version) and VSCode ready. If not, go and get them first.

Now without further ado, here are the steps for a simple debug:

Step 1

Open VSCode and search the official Go plugin from Microsoft team and then install it.

Step 2

Update the tools.
In VSCode, click View -> Command Pallette or do a Ctrl + Shift + P to open the pallette and type

Go: Install/Update Tools

Step 3

Create a simple project. Just create a file named main.go for instance, as shown below. You will use this file as the main entry point for Go to run.

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

func main() {
    resp, err := http.Get("https://checkip.amazonaws.com")
    if err != nil {
        log.Fatalln(err)
    }
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatalln(err)
    }
    stringBody := string(body)
    fmt.Println(stringBody)
}

Step 4

Create breakpoints on the file you just created. Refer to this documentation if you have never done it before.

01.png

Step 5

On VSCode, now open debug menu or click CTRL + Shift + D. Click create a launch.json file and choose Go: Launch package when a prompt is open. You will have a new folder .vscode with launch.json created inside the folder.

Here's how launch.json looks like:

{
    // 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": "debug",
            "program": "${workspaceFolder}"
        }
    ]
}

Step 6

Now that you have the debug configuration file, it's time to try debug mode.
In Debug Menu, click the Launch button or just press F5 key.

02.png

If everything is correct, you will see the debug toolbar floating on the top right of the screen, and DEBUG CONSOLE will show that the debugger is listening.

03.png

Congratulations, now you can do whatever you want, like click F5 to proceed to the next breakpoint or just step-into with F11. ✨✨

Homework

I suppose I will still look into how we can actually achieve how to debug AWS SAM Lambda with Go. For the time being, I've asked for help at the official Slack channel.

Let's see if I can manage to close this later.

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0