atexit

package module
v0.0.0-...-07b9533 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2023 License: MPL-2.0 Imports: 8 Imported by: 0

README

atexit

import "github.com/richardwilkes/atexit"

Go Reference Go Report Card

Provides functionality similar to the C standard library's atexit() call.

package main

import (
    "fmt"

    "github.com/richardwilkes/atexit"
)

func main() {
    atexit.Register(func() { fmt.Println("Goodbye!") })
    atexit.Register(func() { fmt.Println("Preparing to exit.") })
    doSomeStuff()
    // Always end your program with a call to atexit.Exit().
    // Because we know doSomeStuff() always exits in this example, this isn't actually needed here.
    atexit.Exit(0)
}

func doSomeStuff() {
    fmt.Println("in doSomeStuff()")
    fmt.Println("going to do the equivalent of os.Exit(1)")
    atexit.Exit(1)
}

func goodbye() {
    fmt.Println("Goodbye!")
}

Output:

in doSomeStuff()
going to do the equivalent of os.Exit(1)
Preparing to exit.
Goodbye!

atexit.Exit() runs any registered exit functions in the inverse order they were registered and then exits with the specified status. If a previous call to atexit.Exit() is already being handled, the call does nothing but does not return. Recursive calls to atexit.Exit() will trigger a panic, which the exit handling will catch and report, but will then proceed with exit as normal. Note that once atexit.Exit() is called, no subsequent changes to the registered list of functions will have an effect (i.e. you cannot atexit.Unregister() a function inside an exit handler to prevent its execution, nor can you atexit.Register() a new function).

Documentation

Overview

Package atexit provides functionality similar to the C standard library's atexit() call.

Index

Constants

This section is empty.

Variables

View Source
var (
	// RecoveryHandler will be used to capture any panics caused by functions that have been installed when run during
	// exit. It may be set to nil to silently ignore them.
	RecoveryHandler errs.RecoveryHandler = errs.Log
)

Functions

func Exit

func Exit(status int)

Exit runs any registered exit functions in the inverse order they were registered and then exits with the specified status. If a previous call to Exit() is already being handled, this method does nothing but does not return. Recursive calls to Exit() will trigger a panic, which the exit handling will catch and report, but will then proceed with exit as normal. Note that once Exit() is called, no subsequent changes to the registered list of functions will have an effect (i.e. you cannot Unregister() a function inside an exit handler to prevent its execution, nor can you Register() a new function).

func Register

func Register(f func()) int

Register a function to be run at exit. Returns an ID that can be used to remove the function later, if desired.

func Unregister

func Unregister(id int)

Unregister a function that was previously registered to be run at exit. If the ID is no longer present, nothing happens.

Types

This section is empty.

Jump to

Keyboard shortcuts

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