Intro

Each good programming language has at least one good debugger.

Being a systematic debugger is consider nowadays a fundamental quality of good programmers.
Obviously debugging a program it doesn’t mean that you have to use the IDEs and a debugger tool.It’s about having the right mental approach and a good strategy to fetch the errors, instead of fishing and guessing each time by changing your program.

In this article I’ll show how to debug Go with IntelliJ IDEA 14 by using GDB, the powerful “GNU Project Debugger” used to debug golang.

The process that I’m going to explain is for Mac OSX environment.

After completed this procedure you should be able to debug your GoLang executable.

(Step 0) Don’t be a monkey

For a long time I’ve followed procedures without think what I was doing.

I do believe that is really useful understanding the “big picture” behind the scene.

Basically we want debugging with an IDE a program written with some programming language.

We need to help the IDE to do this simple tasks:

  • identify where our SDK is (in this case Go SDK, used to compile and building Go projects);
  • identify where the debug library is located in our machine (GDB binaries).

The first step can be achieved by installing the GoLang plugin provided by Intellij.

The second step can be achieved by creating a Debug configuration where we have to specify which is the program to execute and where is the located the GDB library in our machine.

Prerequisites

Have installed Go in your Mac OSX, with a proper $GOPATH. The entire processes is well explained in the Go documentation.

> go version
go version go1.4.2 darwin/amd64

> which go
/usr/local/go/bin/go

> echo $GOPATH
/Users/danieledangeli/Documents/coding/goWorkspace

Installing Intellij Idea

For this article I used the latest version of Intellij Idea 14.1 .

You can just download it and install in your local system.

Installing Intellij GoLang plugin

Once installed Intellij Idea, the next step is to install the plugin provided by Intellij for Go Lang.

There are several ways to install Intellij’s plugins.

 

In this case I’ve just downloaded it from here:

I used the version 0.9.15.3

Once downloaded, just install it by going on “Preferences > Plugins” , click on “Install from disk” and select the downloaded JAR file.

pluginFromDisk

[Important UPDATE]

Schermata 2015-06-21 alle 08.51.27

Thanks to @disniper, I realized the best way  to install the plugin is to add the “alpha” or the nightly” repository. The alpha will be updated every week and the nightly once a day.

From the previous screenshot, instead of click on “Install plugin from disk” just click on “browse repositories” and then add one of the following link:

Restart Intellij and that’s it.

Configuring Go SDK

After the plugin has been installed, we need to configure the Go SDK.

This steps are really simple, as showed below.

settings

gosdk

 

Schermata 2015-05-31 alle 10.44.11

 

Once completed the installation of Golang plugin, you should be able to write golang code with the IDE features, like auto complete, methods suggestions and so on.
If something doesn’t work in this phase, try to review your workspace style which have to follow the line guides provided by Go.

Install Gdb

I found this article really useful to install GDB on your Mac OSX.

Once installed, we have to setup a debug configuration on Intellij, as showed below.

The self certifications sign problem

Although the configuration is finished, I found one problem ah this phase.

The console showed me an error like:

Starting program: /x/y/foo
Unable to find Mach task port for process-id 28885: (os/kern) failure (0x5).
(please check gdb is codesigned - see taskgated(8))

In a nutshell:

The gdb program must be signed with a self certificate on your mac osx.

Basically, we have to sign our gdb installation, by typing this command on your terminal:

sudo codesign -s gdb-cert /usr/local/bin/ggdb

Before that we need to generate a self-signed certificate.
Following this simple guide will help you to do this process on your Mac OSX machine.

Now you should able to debug your GoLang Code, by running the debug Main Configuration created before.
Schermata 2015-05-28 alle 22.54.27