Simple Go debug with VSCode

I'm writing this article out of frustration after trying without success debugging Go on VSCode with [AWS SAM](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) Lambda local. I then changed to a simpler Go debug and it's enough development at the moment. This proves that [KISS](https://en.wikipedia.org/wiki/KISS_principle) works without a doubt 😅.

#Background
What was supposed to be a simple task of setting [step debugger for AWS SAM with Go programming language](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-debugging-golang.html) 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](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-debugging-nodejs.html) 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](https://marketplace.visualstudio.com/items?itemName=golang.Go) 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.

```go
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](https://code.visualstudio.com/docs/editor/debugging) if you have never done it before.

![01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1055600/4ebdd28a-decb-8ad1-9627-22f5e503dcfa.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:

```shell
{
    // 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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1055600/5cdf4e99-ec4d-caf0-8b30-87a4613799b4.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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1055600/0d41c9aa-e818-6ebe-bd69-74a4bb294f3c.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.