7

I wanted to know how one can enumerate the names and values of all local variables in golang. This is done with the purpose of effectively debugging go code.

Yes, I am well aware golang has limited gdb support right now.

This commonly used thread on stackoverflow has no mention of golang.

I am genuinely looking for a solution (I think others are curious as well).

Many thanks.

Community
  • 1
  • 1
user2324712
  • 425
  • 2
  • 12
  • Short of reflecting on your stack frame, finding the code and parsing it, I doubt it is possible. Even then I'm not sure you could get their values via reflection. Just their names. – captncraig Feb 13 '15 at 02:49
  • I don't think you can do it. – twotwotwo Feb 13 '15 at 07:14
  • update: delve, a debugger for golang. is now in pre 1.0 and has osx support. its still not fully functional but its coming along. You should check it out. – user2324712 May 27 '15 at 21:04

2 Answers2

2

There is no easy solution:

  • gotype can do static analysis of the code and print all variables (but not their value)
  • go-spew can print a variable value (deep pretty printer for Go data structures to aid in debugging), but that is like an enhanced printf, not a 'gdb' like debug session.
VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755
2

There is dlv, you can experiment with the print command, it's the closest thing to a live debugger you can get atm.

Otherwise gotype/go-spew like @VonC mentioned are the way to go.

OneOfOne
  • 88,915
  • 19
  • 172
  • 173