在Golang中打印所有局部变量

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.

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.

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.