Debug a Go application on Visual Studio Code

Viewed 807

I am trying to debug a Go application on Visual Studio Code and it does not seem to work for me. Here is the directory tree.

Tree

Running flow:
In order to run the server, I first run the following command on a terminal

go run -tags=jsoniter cmd/server/*

After that, I hit the API via Postman to run individual services.

Debugging flow:
Likewise, I would like to debug the services (via API hit). After adding some breakpoints, I followed the instruction by creating a launch.json file in the .vscode directory.

Here is the configuration in the launch.json

{
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "cmd/server/",
            //"host": "127.0.0.1",
            //"port": 8080,
        },

After setting up, I clicked "Run" to start the server, but it gave the following error: Failed to launch: could not launch process: can not run under Rosetta, check that the installed build of Go is right for your CPU architecture

I went on some websites and they said that it could be because I used the wrong Go version for the Apple M1 chip. I checked and I used the correct version as suggested but I still encountered the same issue

$go version
>go version go1.17.3 darwin/arm64

Bonus: If I uncomment the two configuration lines "host" and "port", the VCS will tell me that the file is wrong. When I comment on these two lines, it will try to start a server at a random port of 57169. But of course, it will fall into the above error.

Questions:

  • Is there anything wrong with my debugging? Currently, I have to debug by adding log into the code and tracking them. It's not the most efficient I reckon.
  • If VCS does not work for me (unfortunately), would you suggest another IDE/approach for debugging?

Update: I tried this on GoLand as someone suggested it and it did not work for me as well. I received the same error

1 Answers

I created a simple handler and tried to debug on vscode running on Apple M1 chip. Debugger is working for me. I was able to attach debugger to a running process.

Go Version

launch.json configuration (Here setting processId: 0, means vscode will give us an option to select process we want to debug)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Connect to server",
            "type": "go",
            "request": "attach",
            "mode": "local",
            "processId": 0,
        }
    ]
}

main.go file:

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
    a := r.URL.Path
    fmt.Fprintf(w, "Hello, %s!", a[1:])
}

Directory Structure:

Directory Structure

Building and running:

Building and running application

Start Debugging: (Option to select process, my process name is vscode-go)

Start Debugging

Debugger Working when making a request to localhost:8080/test:

enter image description here

More details about attaching debugger to running process can be found here

Related