I’m currently getting my feet wet with Go. I quite like the language but when it comes to debugging I I’m still trying to get warm with what it offers. I’m just not the “printf” kind of guy. I want a real debugger which I can use to step through the program flow to investigate (and manipulate) state.
Go programs can be debugged using the gdb debugger. I’m slowly getting warm with it and I thought I share some advice.
1. There is a Sublime plugin for gdb but my current feeling is that I rather use gdb directly on the command line
2. create such two aliases for your command line:
alias god='go build -gcflags "-N -l"' alias godrun='go build -gcflags "-N -l" -o debug && gdb debug'
god command just compiles your go app with all useful debug flags and uses the default program name for the output file. The
godrun command uses a hardcoded name
debug as the output file name and directly feeds it to
gdb so it’s as easy as calling
godrun to start debugging. Yay!
debug on your
.gitignore file to not commit this special debug build to your repository
4. If you want to see the value of, say, a struct behind a pointer the syntax in gdb is
p *'&varName' (yep asterisk outside the quotes, then the
varName in quotes with an ampersand prepended).
5. Each print in
gdb assigns the printed variable to a temporally $-prefixed variable (e.g.
$5) which can be used from then on (e.g.
$5.SomeStructProperty + 1000
6. Variables can be manipulated with
set variable varName=value (e.g.
set variable varName=1000 or
set $5.SomeStructProperty = 1000)
Those are just random findings and might be totally obvious if you ever worked with gdb before. However, chances are that if your are looking into Go, you are actually NOT coming from the lower level language eco system (e.g. C/C++) but rather from higher level eco systems such as Node.js or .NET.