Debug Go Code with Visual Studio Code

Debug Go Code with Visual Studio Code

Recently, I started debugging Go code using Visual Studio Code. Some minor hiccups aside, it was pretty trivial to get up and going. This is the short version of what you need to do to get started.


If you’re familiar with step-through debugging in PHP apps, you’ll know that, while once quite challenging, it has gotten significantly easier in recent years. With the release of XDebug 3 on January 4th, 2021, it’s now almost as simple as just setting a breakpoint and reloading a page or making another API request.

But what about when writing Go code?

While I’ve spent an ever larger amount of time over the last 6-12 months, writing Go code, I’ve never used step-through debugging with it — until yesterday!

I was working through feedback for an upcoming Twilio tutorial, where the reviewer said that an error message which the app rendered was different to what was stated in the tutorial. Running the app confirmed that he was correct. However, looking at the code suggested otherwise.

Here’s what I should have seen:

Given my confusion, I wanted to know what the error message variable contained. I could have logged the value to the console or to a log file using log.Printf(), but that’s a bit of a rough approach. What’s more, the logging code might accidentally be committed to version control, causing confusion at some time in the future. Given that, I decided to use step-through debugging.

Here’s how I got started. Firstly, I checked to make sure that I had a Go debugger installed (Delve comes with VS Code’s Go extension). You can do this by running the following command.

which dlv

If Delve is installed, you’ll see the path to it printed to the terminal. If not, follow the installation instructions to install it.

Secondly, I created a debugging launch configuration file; the file’s stored in .vscode/launch.json. I did that by clicking Run > Start Debugging, which provided me with a stub configuration, ready to be expanded. From there, I clicked Add Configuration in the bottom right hand corner, and from the pop-up chose Go: Launch Package.

Then, with the configuration in place and the file saved, I clicked Run > Start Debugging again. If you’re on macOS you could use ⇧+⌘+P. If you’re using Microsoft Windows, press F5. Or, if you’re using Linux, press Ctrl+Shift+P or F5.

The debug console appeared at the bottom of the main VS Code window, and confirmed that DLV was running and ready.

Then, I enabled a breakpoint on the variable that I wanted to inspect. No point using step-through debugging if you’re not going to debug anything.

At this point, I could use all of the usual debugging functionality, such as viewing, adding, and removing watches, stopping on breakpoints, stepping-through, stepping into, and stepping over functions.

I was blown away with just how easy it was to use. The way that VS Code changes when you’re in debug mode is very intuitive, yet also very familiar.

That’s the essentials of how you start step-through debugging of Go code in Visual Studio Code

A big hat tip from me to the Go and VS Code teams and communities for making the process so easy and feature-rich — and almost trivial to use.

If you want to know what your Go code is doing at a given moment in time, make sure you use this functionality. It’s excellent and very insightful.


You might also be interested in these tutorials too...

Live Reload Go Projects with wgo
Fri, Apr 19, 2024

Live Reload Go Projects with wgo

Building web apps in Go is extremely rewarding. However, as Go’s a compiled language, to see changes, you need to restart the app. Needless to say that’s quite tedious! With live reloading, it doesn’t need to be.

Restrict HTTP Request Methods in Go 1.22
Thu, Apr 11, 2024

Restrict HTTP Request Methods in Go 1.22

Restricting HTTP request methods, when working with net/http prior to Go 1.22 was a little complicated. However, that’s all changed. Now, it’s pretty trivial. In this short tutorial, I’ll show you the new approach.

Restrict HTTP Request Methods in Go 1.22
Thu, Apr 11, 2024

Restrict HTTP Request Methods in Go 1.22

Restricting HTTP request methods, when working with net/http prior to Go 1.22 was a little complicated. However, that’s all changed. Now, it’s pretty trivial. In this short tutorial, I’ll show you the new approach.

Go mod tidy - A Quick Introduction
Fri, Apr 5, 2024

Go mod tidy - A Quick Introduction

What is go mod tidy and why you would use it? In this short tutorial you’ll get a brief introduction to what it is and see the essentials of how to use it.


Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus