Martin Mai
Aug 24, 2017 · 1 min read

I had to work around this behavior just recently the same way you did, by deferring the call to os.Exit. By now, I prefer to have the main function just call a second function that returns an int and pass that to os.Exit [1].

While I agree that commonly imported packages should refrain from using log.Fatal in their functions, I don’t think that removing it in Go 2.0 is a viable solution. It would probably break *a lot* of existing main functions for no real reason, since main functions won’t be imported by other packages.

I think the easiest way, not to fall into the os.Exit trap is to reduce the length of the main function to the absolute minimum, so there is never a situation where connections or file handles might stay open in the first place. Keeping functions short and concise also sound pretty “goish” to me :)

Great article though, really got me thinking, thanks!

[1] https://stackoverflow.com/a/39924639

)

    Martin Mai

    Written by