Debugging golang with IDE support in vs code on ubuntu

Getting Started

I’ve been dipping my toes into go development lately. Up to this point I’ve debugged my applications through the ancient practice of console printing. But no more.

If you haven’t already, I’d suggest you start by following this guide as they are probably more informed than I am. However, I ran into a few speed bumps which is why I’m here.

The guide linked above assumes you have vscode and the go extension installed. That seems reasonable; I’ll assume the same. I also had golang-golang-x-tools installed from apt. That package may or may not be required.

In vscode, press Ctrl + Shift + P to launch the command palette. Start typing and select Go: Install/Update Tools.

This is where I hit my first bump.

Speed Bump #1

./variables.go:81: vrdr.dwarf.Ranges undefined (type *dwarf.Data has no field or method Ranges, but does have dwarf.ranges)

You’ve got to appreciate a user friendly error message. A quick search brought me here where I learned that my go version was a bit out of date. You can check your version with go version. Currently Ubuntu 16.04 ships with go 1.6 by default while 1.10 is available.

It’s an easy fix, though, right? The newer package is available, it’s just not installed by default.

sudo apt purge golang-go
sudo apt install golang-1.10-go

Speed Bump #2

The 1.10 package doesn’t add itself to your path so now the go command fails.

You can confirm 1.10 installed correctly by running

/usr/lib/go-1.10/bin/go version

Assuming that worked, pull up ~/.bashrc in a text editor and add these lines

# specify your go workspace
export GOPATH=/path/to/your/go/workspace

# add go 1.10 binaries in your $PATH
export PATH=$PATH:/usr/lib/go-1.10/bin

# (optional) include all go apps on your path
export PATH=$PATH:$(go env GOPATH)/bin

At this point you should be able to run go version and see go version go1.10 linux/amd64.

Shut down vscode then reload your ~/.bashrc file with this command

source ~/.bashrc

Launch vscode again. If it complains about not finding go, you may need to log out and login again. Back in vscode, press Ctrl + Shift + P to launch the command palette. Start typing and select Go: Install/Update Tools.

This command should now succeed and you’re so intuitive you’ve stopped reading this post. That’s okay. You should really follow the official guide at this point anyway.

Speed Bump #3

The aforementioned guide creates launch.json with "program": "${fileDirname}". This resolved to my .vscode folder, which clearly did not contain any go files. It was a quick fix to change it to "program": ${fileDirname}/..". After that, I was dropping breakpoints and stepping over lines like a real developer.

Hopefully this post is helpful for someone before the end of this month when 18.04 is released along with newer go packages and this entire post becomes obsolete.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>