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.
These debugging features are possible by using Delve, the Go debugger.
Read Debug Go programs 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.
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:
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:
- In the the Run view, click "
create a launch.json file
" - 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.
e.g.
// .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch API",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceRoot}/main.go",
"args": []
"env": {},
}
]
}
There are many configuration attributes (see the Launch.json 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.
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.
ββ 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.
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! π₯
β Support My Work β
If you enjoy my work, consider buying me a coffee! Your support helps me keep creating valuable content and sharing knowledge. β
Top comments (1)
I miss the explanation where you can start the debugger with parameters for your program. (and I also always want to start debugging via the root of my program, like in the following example)