如何在 go 错误中嵌入退出代码,将其冒泡,然后在处理错误时使用退出代码退出?
例如:
func main () {
foo, err := foo()
if err != nil{
fmt.Printf("error thrown, exit code: %v", err )
}
// I can parse the exit code out of err, but that seems wrong...
// how can I exit with the exit code cleanly? do i need to create a custom error that I bubble up and handle?
os.Exit(1)
}
func foo() (string, err) {
bar, err := bar()
if err != nil {
return "", fmt.Errorf("foo fails b/c bar fails %v": err)
}
}
func bar() (string, err) {
exitCode := 208
return "", fmt.Errorf("some error happened in bar, exit code: %v", exitCode)
}
红颜莎娜
相关分类