Running/Debugging golang with vscode

Hi Guys, In this I will share my local development setup I use to test/develop golang programs with vscode in windows environment(should also be helpful in linux and mac as well).

One way we could do this is with integrated terminal.

go run . 

This works fine for running once but becomes quite irritating when you make any changes and you want see those changes reflected.

And not to mentioned very difficult to debug(basically adding breakpoint does not work). There are some ways around adding support for debugging when golang programs are run from in this way but that does not seems very straightforward to me.

To make these thing work smoothly we can use vscode’s tasks and launch.

Lets first understand what is vscode tasks and launch configuration are.

Vscode tasks are exaclty what it sounds like, array of individual tasks(command to be executed). Launch configs are created to attach debugger to application running on your machine or remote machine.

I have configured below tasks according to windows subsystem for linux or wsl configuration. but same principle should apply for any system

  1. Create vscode task to build our golang binary and copy any configuration file needed along with it(eg. .env file)
  2. Create vscode launch configuration to launch/run our binary build in step 1.
  3. Add preLaunchTask to launch configuratoin so that golang binary gets built automatically(i.e. before our app runs so that we have latest change always)

So lets get started with task.

To add task in vscode, first lets create directory called “.vscode” (not starting dot in folder name)

After that create tasks.json file and add following content

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "go-build",
            "type": "shell",
            "command": "go build -o bin/server -gcflags=all=\"-N -l\" . && cp .env* bin/",
        }
    ]
}

(I have added command “cp .env* bin/” just to demostrate that we can needed files in bin directory, its not required in this application)

Above file creates task in vscode which we can run by opening command pallet(cmd + shift + p) and typing “tasks: run command” which will give us option to select our task that we want to run(in our case we have named our task as “go-build”).

Next lets create launch.json file with following command which will allows to run our task and debug golang application inside vscode.

{
    // 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 Sample-App",
            "type": "go",
            "request": "launch",
            "mode": "exec",
            "program": "${workspaceFolder}/bin/server",
            "preLaunchTask": "go-build",
            "env":{}
        }
    ]
}

Important thing to note in above configuration is the preLaunchTask which runs our “go-build” task(which basically builds our binary and stores it in bin folder)

After this, you can go to debug menu and then click on green play button to start debugging. or press F5 to start debugging and SHIFT + F5 to stop debugging.

Once you run application and attach debugger you can get debug your application and analyize your application

Github repo : ashishmaurya/golang-vscode-sample-debug: Sample vscode app with vscode setting to allow debugging (github.com)

Leave a Reply