如何调试Go程序?我一直在使用Gedit Go IDE,但它没有调试.有没有办法通过我的代码和检查内存?还是我坚持打印报表?我可以使用OutputDebugString吗?
nmi*_*els 22
更新:现在有关于使用GDB调试Go Code的文档中的官方页面.自从撰写这个答案以来,已经发生了很大变化,下面列出的一些限制已被删除.我将剩余的答案留给后代,但如果你想调试Go代码,请点击上面的链接.
Go链接器现在发出可由gdb版本7.x解释的DWARF调试符号.
从上面链接的博客文章中突出显示:
您可以...
还有一些不便之处:
有些事情不起作用:
Von*_*onC 13
新举措(2014年5月开始): derekparker/delve
Delve是一个用Go编写的Go调试器.
(主要针对Linux而言,OsX支持即将到来,Windows支持未知2016年支持)
- 附加到已经运行的进程
- 启动进程并开始调试会话
- 设置断点,单步,跳过函数,打印变量内容
调试器可以通过三种方式启动:
一步编译,运行和附加:
$ dlv -run
Run Code Online (Sandbox Code Playgroud)
提供要调试的程序的名称,调试器将为您启动它.
$ dlv -proc path/to/program
Run Code Online (Sandbox Code Playgroud)
提供当前正在运行的进程的pid,调试器将附加并开始会话.
$ sudo dlv -pid 44839
Run Code Online (Sandbox Code Playgroud)
Delve可以在调试会话中通过breakpoint命令插入断点,但是为了便于调试,您也可以调用
runtime.Breakpoint()Delve来处理断点并在下一个源代码行停止程序.
Another initiative for go debugging session: hopwatch
Unlike most debuggers, hopwatch requires you to insert function calls at points of interest in your program. At these program locations, you can tell Hopwatch to display variable values and suspend the program (or goroutine).
Hopwatch uses Websockets to exchange commands between your program and the debugger running in a HTML5 page.
(so it is still similar to "print statement" but with a more elegant way to see the result without polluting stdout and stderr)

When your program calls the Break function, it sends debug information to the browser page and waits for user interaction.
Using the functionsDisplay,PrintforDump(go-spew), you can log information on the browser page.
On the hopwatch page, the developer can view debug information and choose to resume the execution of the program.

也许一些一步一步的GDB入门指示会有所帮助.
我创建了silly.go包含:
package main
import "fmt"
func x() {
foo := 5
fmt.Printf("foo: %v\n", foo)
}
func main() {
go x()
fmt.Printf("Done.\n")
}
Run Code Online (Sandbox Code Playgroud)
运行后8g silly.go和8l -o silly silly.8,我可以运行gdb silly.(我有"GNU gdb(Ubuntu/Linaro 7.2-1ubuntu11)7.2"据我所知,Ubuntu 11.04 32位.)
然后我可以打字list,b 7(简称break 7)和run.它停在第7行,我可以运行:
(gdb) p foo
$1 = 5
Run Code Online (Sandbox Code Playgroud)
看看Eclipse/CDT调试器和/或DDD是否适用于Go会很有趣.
| 归档时间: |
|
| 查看次数: |
20274 次 |
| 最近记录: |