Documentation
¶
Overview ¶
Package atexit lets you define handlers when the program exits.
Add handlers using Register. You must call atexit.Exit to get the handler invoked (and then terminate the program).
This package also provides replacements to log.Fatal, log.Fatalf and log.Fatalln.
Example:
package main
import (
"fmt"
"time"
"github.com/patarapolw/atexit"
)
func handler() {
fmt.Println("atexit triggered")
}
func main() {
atexit.Listen() // Await for SIGINT, SIGTERM, whatever. Also works in Windows
defer atexit.ListenPanic() // Listen for panic and crashes
atexit.Register(handler)
time.Sleep(1 * time.Minute)
atexit.Exit(0) // This also needs to be called at the end of main function, if you want atexit to be executed on normal exit.
}
Index ¶
Constants ¶
const (
// Version is package version
Version = "0.4.0"
)
Variables ¶
This section is empty.
Functions ¶
func Exit ¶
func Exit(code int)
Exit runs all the atexit handlers and then terminates the program using os.Exit(code)
func Fatal ¶
func Fatal(v ...interface{})
Fatal runs all the atexit handler then calls log.Fatal (which will terminate the program)
func Fatalf ¶
func Fatalf(format string, v ...interface{})
Fatalf runs all the atexit handler then calls log.Fatalf (which will terminate the program)
func Fatalln ¶
func Fatalln(v ...interface{})
Fatalln runs all the atexit handler then calls log.Fatalln (which will terminate the program)
func ListenPanic ¶ added in v0.4.0
func ListenPanic()
func RunHandlers ¶ added in v0.4.1
func RunHandlers()