os.Exit()
Use os.Exit
to immediately exit with a given status.
package main
import (
"fmt"
"os"
)
func main() {
defer fmt.Println("!") // never called
os.Exit(3)
}
- Conventionally, code zero indicates success, non-zero an error
defer
s will not be run when usingos.Exit
, so thisfmt.Println
will never be called.- For portability, the status code should be in the range [0, 125].
// Exit causes the current program to exit with the given status code.
// Conventionally, code zero indicates success, non-zero an error.
// The program terminates immediately; deferred functions are not run.
//
// For portability, the status code should be in the range [0, 125].
func Exit(code int) {
if code == 0 {
// Give race detector a chance to fail the program.
// Racy programs do not have the right to finish successfully.
runtime_beforeExit()
}
syscall.Exit(code)
}
Obtain Exit Code
Note that unlike e.g. C, Go does not use an integer return value from main to indicate exit status. If you’d like to exit with a non-zero status you should use os.Exit.
If you run exit.go using go run, the exit will be picked up by go and printed.
By building and executing a binary you can see the status in the terminal.
$ go run exit.go
exit status 3
$ go build exit.go
$ ./exit
$ echo $?
3
Difference between Panic and os.Exit
Experiment
package main
import (
"fmt"
)
func main() {
defer fmt.Println("defer called")
panic("test")
}
Output:
defer called
panic: test
goroutine 1 [running]:
main.main()
/Users/shiwei/SW/GoPlayground/sw.go:9 +0x95
Process finished with exit code 2
Summary
os.Exit
skips the execution of deferred function.- With
os.Exit
, you can specify the exit code. - The exit code of
panic
is always 2 - panic somewhere can be caught and ignored or logged using
recover
, while no way to recover ifos.Exit
is called, which makes sense, as when you callos.Exit
you wanna our program exits immediately.
If you need to execute the deferred function, you have no choice but panic
. (On the other hand, if you want to skip execution of deferred function, use os.Exit
.)
Under other conditions:
- Use
panic
when something really wired happens, e.g. programming logic error. - Use
os.Exit
when you want an immediate exit, with specified exit code.
Reference
- https://golang.org/pkg/os/#Exit
- https://gobyexample.com/exit
- https://stackoverflow.com/questions/28472922/when-to-use-os-exit-and-panic
- https://github.com/golang/go/issues/24869