戈朗 : Stack Trace single routine when program crashes

标签 go stack-trace

我只是想知道是否有办法让我的 go 应用程序只输出发生 panic (并随后死亡)的例程的堆栈跟踪,而不是我的所有 goroutine,因为它们有很多。

我假设有某种形式的标志,我可以将其传递给 go run 或 go build 来执行此操作,但似乎无法找到它。

感谢任何帮助。

最佳答案

我可能会尝试(未测试)使用 runtime.Stack在延迟函数中修改(出于调试目的)现有代码以进行调试:

const debug = true //TODO turn off for production

func MyPotentiallyPanickingGoroutine() {
        if debug {
            buf := make([]byte, 1<<16)
            defer func() {
                    fmt.Printf("%s\n", runtime.Stack(buf, false))
            }()
        }

        // existing code follows
}

关于戈朗 : Stack Trace single routine when program crashes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17671090/

相关文章:

sockets - 限制连接到网络服务的客户端数量

multithreading - 即使例程在 Golang 中发生了 "keep main thread running",如何返回 "runtime error"?

go - SQLMock 和 Gorm : Mocking Postgres Insert

ruby - 我如何跟踪我的堆栈跟踪高度以进行调试?

c++ - 共享库中的回溯函数

json - 解析POST请求中发送的json正文数组并打印

sockets - Golang 中的套接字调试器

c# - 什么会导致 throw 重置调用堆栈(我使用的是 "throw",而不是 "throw ex")

调用不带参数的带参数的C函数

Javascript 堆栈跟踪被 chop