打印出Golang中的所有局部变量。

huangapple go评论67阅读模式
英文:

Print all local variables in Golang

问题

我想知道如何在golang中枚举所有局部变量的名称和值。这样做是为了有效地调试go代码。

是的,我很清楚golang目前的gdb支持有限。

这个在stackoverflow上常用的帖子中没有提到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.

答案1

得分: 2

没有简单的解决方案:

  • gotype 可以对代码进行静态分析并打印所有变量(但不包括它们的值)

  • go-spew 可以打印变量的值(用于深度美化打印Go数据结构以帮助调试),但这更像是增强版的 printf,而不是像 gdb 那样的调试会话。

英文:

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.

答案2

得分: 2

有一个名为dlv的工具,你可以尝试使用print命令,它是目前最接近实时调试器的工具。

另外,像@VonC提到的gotype/go-spew也是可行的方法。

英文:

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.

huangapple
  • 本文由 发表于 2015年2月13日 09:50:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/28491257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定