Package dependencies and initialization

go

Solution

Move logging initialization out to a `log` package that is imported by both `main` and `A` and does not depend directly or indirectly on the parts of your app that you want to log to the file during initialization. From the descriptions of init order in Effective Go and the spec, that should be enough to make sure your logging is initialized first thing.

Problem

I'm new to Go and trying to find a solution to the following problem. - The goal is to write all logs to a file. - package main imports package A. - In package main, in the main method, logging is setup to write to a file - package A has an init function. In the init function of A, there is a log line - log.Fatal("I am package A"). - Since main package imports A, A's init function is first called (even before we get a chance to setup the logs to write to a file). How to resolve this so the log "I am package A" gets written to a file?

Original source