如何在执行Go二进制文件的任何操作系统中禁用堆栈跟踪信息

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

How to Disable Stack Trace Info in Go for any OS Executing the Binary

问题

我目前正在使用Go构建一个CLI,并尝试禁用由panic引起的任何回溯。我相信我的代码具有很好的错误处理,但现在想要抑制任何panic消息(对Go相对陌生)。

我目前在我的main.go函数中添加了以下内容(故意引起panic):

var myarr [2]string
myarr[0] = "Foo"
myarr[1] = "Bar"
for i := range myarr {
	fmt.Println(myarr[i+1])
} 

然后我得到了以下结果:

goroutine 1 [running]:
Bar
panic: runtime error: index out of range [2] with length 2

main.main()
        {main.go所在的目录}/main.go:23 +0x207

我该如何抑制这个错误,以便任何拥有可执行二进制文件的人都看不到这个错误?

我尝试在构建二进制文件时使用GOBACKTRACE环境变量,并将其值设置为GOBACKTRACE=none,但这对我测试的其他操作系统没有任何效果。

英文:

I am currently building a CLI using Go and am trying to disable any backtrace that is produced as a result of a panic. I believe my code has great error handling, but would now like to suppress any panic messages (fairly new to Go).

I currently put in the following in my main.go function (to purposely cause a panic):

var myarr [2]string
myarr[0] = "Foo"
myarr[1] = "Bar"
for i := range myarr {
	fmt.Println(myarr[i+1])
} 

And I get this as a result:

goroutine 1 [running]:
Bar
panic: runtime error: index out of range [2] with length 2

main.main()
        {directory where main.go is located}/main.go:23 +0x207

How can I suppress this error such that anyone with the executable binary file will not be able to see this error?

I've tried utilizing the GOBACKTRACE environment variable when building my binary and setting its value to GOBACKTRACE=none, but this has no effect on other operating systems I've tested on.

答案1

得分: 1

我已经尝试在构建二进制文件时使用GOBACKTRACE环境变量,并将其值设置为GOBACKTRACE=none,但是在我测试的其他操作系统上没有效果。

环境变量应该叫做GOTRACEBACK,而不是GOBACKTRACE

此外,你可以使用debug.SetTraceback("none")来达到相同的效果,尽管这仍然可以通过GOTRACEBACK环境变量进行覆盖。

如果你使用了正确的命名,它应该可以工作。如果它不起作用,恭喜你:你发现了一个golang的错误,你可能应该报告它。

英文:

> I've tried utilizing the GOBACKTRACE environment variable when building my binary and setting its value to GOBACKTRACE=none, but this has no effect on other operating systems I've tested on.

The environment variable is called GOTRACEBACK, not GOBACKTRACE.

Also, you can use debug.SetTraceback("none") to achieve the same effect, although this can still be overridden via the GOTRACEBACK environment variable.

If you use the correct naming, it should work. If it does not work, congratulations: you found a bug in golang and you should likely report it.

答案2

得分: 0

根据@Burak的提到,您想要使用内置的Go函数recover。关于panic和recovery的细微差别,有一篇很好的Go博客文章。

如果您想要覆盖整个应用程序堆栈,只需在main级别通过defer函数注册recover

func main() {

    defer func() {
        if r := recover(); r != nil {
            fmt.Println("unexpected problem encountered - aborting")
            // 可选择将`r`记录到异常日志中,以便用户可以将其发送给开发者
            os.Exit(1)
        }
    }()

    run(os.Args...)
}

https://play.golang.org/p/b8qYnlNZsr5

英文:

As @Burak mentioned you want to use the built-in Go function recover. There's a good Go blog post on all the subtleties of panic and recovery.

If you want to blanket cover your entire application stack, then simply register recover via a defer function at the main level:

func main() {

	defer func() {
		if r := recover(); r != nil {
			fmt.Println("unexpected problem encountered - aborting")
            // optionally log `r` to an exception log - so users may email to developer
			os.Exit(1)
		}
	}()

	run(os.Args...)
}

https://play.golang.org/p/b8qYnlNZsr5

huangapple
  • 本文由 发表于 2021年7月12日 05:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/68339988.html
匿名

发表评论

匿名网友

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

确定