Golang Debugging in Visual Studio Code
Tags: go, webdev, vscode, backend
Published: 2023-07-30 09:02:17

## Introduction 💡
Debugging is an indispensable skill for every software engineer, regardless of their experience level. Whether you're a seasoned developer or just starting your journey with Golang, understanding how to effectively debug Golang code using Visual Studio Code (VSCode) can greatly improve your productivity and proficiency. 
**In this blog** ✍️, we will walk through the process of setting up and using the Golang debugging in VSCode.

## Overview
The Go extension allows you to launch or attach to Go programs for debugging. You can inspect variables and stacks, set breakpoints, and do other debugging activities using [VS Code’s Debugging UI](https://code.visualstudio.com/docs/editor/debugging).

These debugging features are possible by using [Delve](https://github.com/go-delve/delve), the Go debugger.

> Read [Debug Go programs](https://github.com/golang/vscode-go/wiki/debugging) in VS Code for setup steps, supported features, configurations, information on remote debugging and a troubleshooting guide. For general debugging features such as inspecting variables, setting breakpoints, and other activities that aren't language-dependent, review [VS Code debugging](https://code.visualstudio.com/docs/editor/debugging).

## Setting Up Golang Debugging in VSCode 🧑‍💻
In this hands-on section, we will walk you through the step-by-step process of setting up Golang debugging in Visual Studio Code. By the end of this section, you'll have a fully configured debugging environment, ready to help you identify and fix bugs in your Golang code.

### Step 1: Set Up a Golang Project
Before we start debugging, we need a Golang project to work with. You can either create a new Golang project or use an existing one. For simplicity, let's create a new Golang project. Open a terminal or command prompt, and create a new directory for your project:

```bash
mkdir my_golang_project
cd my_golang_project
go mod init example.com/my_golang_project
```

### Step 2: Configure the Debug Environment
Now that we have the _Go extension_ installed and a _Golang project_ ready, we need to set up the debugging configuration. In the root directory of your Golang project, create a `.vscode` folder if it doesn't exist, and within it, create a `launch.json` file. This file will define the debugging configurations for your project.

#### To create a configuration file:
1. In the the **Run view**, click "`create a launch.json file`"
2. Choose **Go: Launch Package** from the debug configuration drop-down menu. VS Code will create a `launch.json` file in a `.vscode` folder in your workspace (project root folder) or in your user settings or workspace settings.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gbk6u1eit6h5no89tvqe.gif)

**e.g.**
```json
// .vscode/launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch API",
      "type": "go",
      "request": "launch",
      "mode": "auto",
      "program": "${fileDirname}",
      "args": []
    }
  ]
}
```
> There are many configuration attributes (see [the Launch.json attributes](https://github.com/golang/vscode-go/blob/master/docs/debugging.md#launchjson-attributes)).

### Step 3: Set a Breakpoint and Start Debugging
Open a Golang source file in your project (e.g., `main.go`) and set a breakpoint on the line where you want to pause the program's execution. You can set a breakpoint by **clicking in the gutter to the left of the line number**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77c5x373xnkegafennlu.png)

### Step 4: Start Debugging
Now, click on the "**Run and Debug**" icon in the Activity Bar on the side of the window. It looks like a play button with a bug. In the Run and Debug view that appears, select "**Go: Launch**" from the drop-down menu, and click on the green start button (or press F5) to begin debugging.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92j36cf3vktlq4ef7dxj.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ny6gzj2hhv5plnn4ttcl.gif)

>❗❗ When you start debugging for the first time or if the dlv executable on your system is too old to support DAP, the Go extension may ask to install or update Delve. Follow the instructions to install it, and then start the debugging session again.

### Step 5: Debugging in Action
The program execution will pause at the breakpoint you set. You can now examine variable values, step through the code, and understand the program's flow during runtime. Utilize the debug toolbar and context menu to interact with the debugger and gain insights into your code.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1zljcq1epjrksvbsp8s.png)

#### The available commands are:
- Continue / Pause F5
- Step Over (aka `next` in Delve) F10
- Step Into (aka `step` in Delve) F11
- Step Out (aka `stepout` in Delve) Shift+F11 or ⇧F11
- Restart (currently this is "Stop + Start") Ctrl+Shift+F5 or ⇧⌘F5
- Stop (terminate the debugee. Available in Launch request) Shift+F5 or ⇧F5

 
🥂 **Congratulations!** You have successfully set up Golang debugging in Visual Studio Code and completed your first debugging session. Keep practicing and exploring the various debugging features available to become a proficient Golang debugger. Happy debugging! 🥂