DEV Community

Cover image for Setting up Debugging in Go with VS Code
Tom Holloway 🏕
Tom Holloway 🏕

Posted on • Updated on

Setting up Debugging in Go with VS Code

Go has some really great tools, and most of these are all built-in so you don't have to look very far to do much outside the core language and tooling. Documentation is comprehensive, the language is minimalist in nature, and it is surrounded by a massive community. In this article, I'll show you some companion projects that help you with debugging your go projects.

Delve

There are a few projects you can use to debug your go applications. The one most recommend is Delve as an alternative to the gdb. GDB is the GNU symbolic debugger and prior to delve this was the only way to actually debug Go programs. Luckily, that's not the case anymore and you can now combine Delve with VS Code quite easily.

Installing Delve in Linux

You can use the standard go get method

go get github.com/go-delve/delve/cmd/dlv

Alternatively, you can clone the repo directly and install with make.

$ git clone https://github.com/go-delve/delve.git $GOPATH/src/github.com/go-delve/delve
$ cd $GOPATH/src/github.com/go-delve/delve
$ make install

Installing Delve in mac OS

You will need to make sure you have xcode-select command line tools installed prior to this (as well as Go itself).

xcode-select --install

Once you have that, use the standard go get to install.

go get github.com/go-delve/delve/cmd/dlv

Installing Delve in Windows

Delve can be installed in Windows with the go get method

go get github.com/go-delve/delve/cmd/dlv

Setting up VS Code

You can setup Go in VS Code by making sure you have the vscode-go extension installed. You can do this by:

  • Make sure you have Go installed and it is located by VS Code. Open up the command palette (Ctrl+Shift+P / Cmd+Shift+P) and run Go: Locate Configured Go Tools.
  • Navigate to the extensions (Ctrl+Shift+X / Cmd+Shift+X) and search for "Go". (publisher is golang.Go).

The VS Code extension supports Intellisense, code navigation, debugging, testing, and shows build, lint, vet diagnostics in detail.

Next you'll need to install the dlv tool for vs code itself.

  • Open up Command Palette now (Ctrl+Shift+P / Cmd+Shift+P) and select Go: Install/Update Tools then search/select dlv.

Once you have this, you are ready to start debugging!

Open up the package main or test file you want to debug. Open up the command palette (Ctrl+Shift+P / Cmd+Shift+P) and select Debug: Start Debugging then select Go. This will start things up and hit your breakpoints as you would expect.

Debugging in VS Code

For more details, check out the extension for VS Code and the various integrations for Delve

Best of luck!

Discussion (4)

Collapse
tmack8080 profile image
tmack8080

This should be titled "Setting Up Go Debugging in VSCode" as it does not discuss the actual process of debugging.

Collapse
nyxtom profile image
Tom Holloway 🏕 Author

Fixed! :)

Collapse
amolgautam25 profile image
Amol Gautam

Thank you so much.

Collapse
andreidascalu profile image
Andrei Dascalu

One thing I could never get to work was delve with vscode but inside Docker