Skip to content

cww0614/finally

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

finally

https://img.shields.io/badge/godoc-finally-blue.svg

Makes sure cleanup functions are called in addition to defer.

Example

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.

About

Makes sure cleanup functions are called in addition to defer

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages