Debugging in Go with Delve


I’m not a go developer, yet, although I am considering it more seriously these days. It’s been a week since I started working in the Filestash project, I’ve not yet managed to get things working but in the process I’ve been learning a bit of go.

At some point I really needed to know the internal state of the application at given times, so I decided to explore how to debug with go. I know that many developers stick to string output, but I’ve always been a fan of the debuggers, even for javascript, but I don’t follow the state-of-the-art debugger news. So it didn’t take much time to find about Delve, a really friendly open source go debugger.

I think the documentation could be better structured, but to be fair, I only used it to install it, and after that it was really intuitive and when I needed help I used the help option from the CLI.

Christopher Berge from Applied Go created a nice and more detailed cheatsheet, I will talk very briefly about how intuitive it was for a js developer to use it, and about the things that were not so intuitive.

First, installation was straightforward, I connected to the container instance I am using and installed it directly, I still need to add it to the container file.

As part of a lapsus I wrote break in a source file, just to find a compiler error saying it was not possible to write a break outside a conditional structure or something like that. The easiest way to start Delve was to run it from the directory of the entry-point, in the case of Filestash, it is /cmd, then I run dlv debug, the first time it took some time, but after that it has been really fast.

The breakpoints are added from the CLI without the need to edit the source files, a feature I had never used but its quite nice, you can use the relative path and the line number in the common syntax:

break server/ctrl/session.go:32

Then you need to write continue or c to start the execution, if you want to stop execution you can press ctrl+c at any point. You can quit the program with exit | quit | q and restart execution with restart | r. I think when you just press enter it repeats the last command you used, but I discover that empirically and it may be wrong or incomplete.

Then there are a few handy commands:

  • pb prints breakpoints
  • clear n delete breakpoint n
  • clearall delete all breakpoints
  • p print a variable or evaluates an expression

There are many more commands, but to be honest, since I am debugging a web app under normal circumstances, mostly to learn about it, I only needed those few comments. I was quite happy using it, and it helped me a lot to move faster than I would otherwise done.

One thing I don’t fully understand, but my understanding of go programs is very limited at the moment, is that it seems to use a separated cache/config from the binary program, which is nice, since it allows to experiment freely, but it is something I would love to dig a bit deeper in the days to come.



Source link

Leave a Reply

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