gpt4 book ai didi

戈朗 : Stack Trace single routine when program crashes

转载 作者:IT王子 更新时间:2023-10-29 01:15:46 25 4
gpt4 key购买 nike

我只是想知道是否有办法让我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com