This is a simple example of how to call os.Exit
in go while still allowing
deferred functions to run.
This became necessary for me since I was writting a command line application and needed to convey failures to the user via exit codes, but needed to make sure all of my deferred functions ran.
Per the os.Exit() docs, defered functions
are not called. So this is a work around to make them be called while still
handling panic
s.
See the documentation in the code for more information on how this works.
This application has two command line arguments for testing purposes:
-x CODE
will cause the program to exit with exit code CODE
-p
will cause the program to panic