atexit

package module
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2021 License: MIT Imports: 7 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

atexit

Simple atexit implementation for Go.

Note that you have to call atexit.Exit and not os.Exit to terminate your program (that is, if you want the atexit handlers to execute).

Example usage

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)

	go func() {
		defer atexit.ListenPanic()

		time.Sleep(1 * time.Second)
		panic("panic")
	}()
	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.
}

Caveats

  • os.Exit doesn't call atexit. Use atexit.Exit instead.
  • atexit.Exit(0) needs to be called at the end of main function, if you want atexit to be executed on normal exit.
  • log.Fatal* also don't call atexit. Use atexit.Fatal* instead.
  • panic can be listened, but do call defer atexit.ListenPanic() at the beginning of main function.
  • If panic is inside Goroutine, call defer atexit.ListenPanic() at the beginning of main function, too.

Install

go get github.com/patarapolw/atexit

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

View Source
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 Listen added in v0.4.0

func Listen(signals ...os.Signal) chan os.Signal

func ListenPanic added in v0.4.0

func ListenPanic()

func RunHandlers added in v0.4.1

func RunHandlers()

Types

type HandlerID added in v0.4.0

type HandlerID uint

func Register

func Register(handler func()) HandlerID

Register adds a handler, call atexit.Exit to invoke all handlers.

func (HandlerID) Cancel added in v0.4.0

func (id HandlerID) Cancel() error

Cancel cancels the handler associated with id

Directories

Path Synopsis
examples
crash command
panic command
simple command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址