Makes sure cleanup functions are called in addition to defer
.
func main() {
finally.RegisterShutdownHook()
cleanup := finally.Wrap(func() {
fmt.Println("Cleaning up")
})
defer cleanup()
// do something
}
If the program executes normally, the wrapped function that prints
Cleaning up
will be called in defer as usual.
However, if the program is terminated by signals like SIGTERM,
deferred functions will not run. And in this case, the wrapped
function will be called in the signal handler registered in
RegisterShutdownHook
.
The wrapped function is guaranteed to be called only once, either in
defer
or in signal handlers.