Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print all local variables in Golang

Tags:

go

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.

like image 524
user2324712 Avatar asked Feb 13 '15 01:02

user2324712


2 Answers

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.
like image 119
VonC Avatar answered Nov 08 '22 11:11

VonC


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.

like image 36
OneOfOne Avatar answered Nov 08 '22 09:11

OneOfOne