Exit with error code in go?

后端 未结 5 1700
Happy的楠姐
Happy的楠姐 2021-02-01 12:21

What\'s the idiomatic way to exit a program with some error code?

The documentation for Exit says \"The program terminates immediately; deferred functions a

5条回答
  •  旧时难觅i
    2021-02-01 12:48

    Yes, actually. The os package provides this.

    package main
    
    import "os"
    
    func main() {
        os.Exit(1)
    }
    

    http://golang.org/pkg/os/#Exit

    Edit: so it looks like you know of Exit. This article gives an overview of Panic which will let deferred functions run before returning. Using this in conjunction with an exit may be what you're looking for. http://blog.golang.org/defer-panic-and-recover

提交回复
热议问题