如何调试Go语言编写的程序?
如何调试 Go 程序?我一直在使用 Gedit Go IDE,但它没有调试功能。有没有办法单步执行我的代码并检查内存?或者我被打印语句困住了?我可以使用 OutputDebugString 吗?
How do I debug a Go program? I have been using the Gedit Go IDE, but it doesn't have debugging. Is there a way to step though my code and inspect memory? Or am I stuck with print statements? Can I use OutputDebugString?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
更新:文档中现在有一个关于使用 GDB 调试 Go 代码。自编写此答案以来发生了很多变化,并且下面列出的一些限制已被删除。我将把这个答案的其余部分留给后人,但如果你想调试 Go 代码,请点击上面的链接。
Go 链接器现在发出 DWARF 调试符号可由 gdb 版本 7.x 解释。
上面链接的博客文章中的重点内容:
您可以...
仍然存在一些不便之处:
有些东西不起作用:
Update: There is now an official page in the docs on Debugging Go Code with GDB. Much has changed since this answer was written, and several of the limitations listed below have been removed. I'm leaving the rest of this answer for posterity, but if you want to debug Go code, follow the link above.
The Go linkers now emit DWARF debugging symbols that can be interpreted by gdb version 7.x.
Highlight from the blog post linked above:
You can...
There are still some inconveniences:
Some things don't work:
新举措(2014 年 5 月开始):
derekparker/delve
< /a>,现在(2021 年):go-delve/delve:功能
用法
断点
New initiative (started May 2014):
derekparker/delve
, now (2021): go-delve/delve:Features
Usage
Breakpoints
Go 调试会话的另一个举措:hopwatch< /strong>
(所以它仍然类似于“打印语句”,但有一种更优雅的方式来查看结果,而不会污染
stdout
和stderr
)Another initiative for go debugging session: hopwatch
(so it is still similar to "print statement" but with a more elegant way to see the result without polluting
stdout
andstderr
)也许一些有关 GDB 入门的分步说明会有所帮助。
我创建的silly.go包含:
运行
8gsilly.go
和8l -osillysilly.8
后,我可以运行gdbsilly
。 (据我所知,我有“GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2”,它随 Ubuntu 11.04 32 位一起提供。)然后我可以输入
list
,b 7(
break 7
的缩写)和run
。它停在第 7 行,我可以运行:看看 Eclipse/CDT 调试器和/或 DDD 是否可以与 Go 一起工作会很有趣。
Perhaps some step by step instructions for getting started with GDB would help.
I created silly.go containing:
After running
8g silly.go
and8l -o silly silly.8
, I can rungdb silly
. (I have "GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2" that as far as I know came with Ubuntu 11.04 32 bit.)I can then type
list
,b 7
(short forbreak 7
), andrun
. It stops at line 7, and I can run:It would be interesting to see if the Eclipse/CDT debugger and/or DDD would work with Go.
GDB 7.5 正式支持 Go。
GDB 7.5 officially supports Go.
有一个名为 ogle 的实验性调试器包。不确定它的效果如何。
There is an experimental debugger package called ogle. Not sure how well it works.
不幸的是,但目前最好的方法是使用打印功能。内置的 print 和 println 可以工作,但 fmt 中的函数有时会工作得更好,具体取决于您要查找的信息。
It's unfortunate, but the best way right now is to use print functions. The built-in print and println will work, but the functions in fmt will sometimes work better depending on what information you're after.
另一种正在开发的调试技术(2014 年第 4 季度):Go Execution Tracer
Another debug technique being developed (Q4 2014): Go Execution Tracer
获取JetBrains Toolbox,下载GoLand,点击编辑器左侧,即可会设置一个断点。
Get the JetBrains Toolbox, download GoLand, click on the left side of the editor, and it'll set a breakpoint.