Perfect func main()

Wed, Nov 16, 2016 One-minute read

It’s the only one function all Go commands must have. You may say that everyone’s main() function is different, depends on a project. But let’s think about reusability and testability. main() function cannot be tested in a good way, also it cannot be imported and used in another go project. So all you code you put into it isn’t reusable/testable.

Instead of having some logic in main() function it’s better to isolate it in some package and just import it. What your command must have is a correct exit code, and main function is the only one place to have it. Why? Because if you put os.Exit() or log.Fatal() into your packages it can break your package. Developers can import this package and they will be unhappy if theirs tests are interrupted by this call (os.Exit() will break test executable).

So let’s see how ideal main() function looks: