Documentation ¶
Overview ¶
Package exit provides an alternative to os.Exit(int).
Unlike os.Exit(int), it won't exit the binary immediately and instead run deferred functions first.
Defer a call to exit.Recover() or exit.RecoverAll() at the beginning of main(). Use exit.Return(int) to initiate an exit.
func main() { defer exit.Recover() ... f() } func f() { exit.Return(123) }
All functions deferred *after* defer exit.Recover()/RecoverAll() will be executed before the exit.
This mechanism only works within the *same* Go routine because it is based on the Go builtins panic() and recover(). Therefore, it is advised to use this package only in the main goroutine.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Recover ¶
func Recover()
Recover should be deferred as the first line of main(). It recovers the panic initiated by Return and converts it to a call to os.Exit. Any functions deferred after Recover in the main goroutine will be executed prior to exiting. Recover will re-panic anything other than the panic it expects from Return.
func RecoverAll ¶
func RecoverAll()
RecoverAll can be deferred instead of Recover as the first line of main(). Instead of re-panicking, RecoverAll will absorb any panic and convert it to an error log entry with a stack trace, followed by a call to os.Exit(255).
Types ¶
This section is empty.