Skip to content
Myr

atexit.go

This file

Path

src/util/atexit.go

28 lines · 554 bytes · Go

Last changed by

Not known.

History of this file

Take it

1package util23import (4	"sync"5)67var atExitFuncs []func()89// AtExit registers the function fn to be called on program termination.10// The functions will be called in reverse order they were registered.11func AtExit(fn func()) {12	if fn == nil {13		panic("AtExit called with nil func")14	}15	once := &sync.Once{}16	atExitFuncs = append(atExitFuncs, func() {17		once.Do(fn)18	})19}2021// RunAtExitFuncs runs any functions registered with AtExit().22func RunAtExitFuncs() {23	fns := atExitFuncs24	for i := len(fns) - 1; i >= 0; i-- {25		fns[i]()26	}27	atExitFuncs = nil28}