Here is my take. I’m very open to suggestions. In my latest PR, I am trying to emulate this “GoogleInit” function. Here is roughly how it works.
There are two rounds of initialization. First round is the package init. Things are not fully initialized yet, so you want to be more careful with what you do here. For example, flags are not ready. But what you can do is to call x.AddInit(func() {…}). Then in every main, we would call x.Init at the very start to (1) initialize flags, then (2) run all the functions added by x.AddInit. In the second round of init, the flag values are ready, so you can now initialize all those global vars.
This way is much cleaner. Especially, if you want to use global flags in
your init() function.
For now, will update my PR to use normal init() sequence.
But we should probably follow your method globally to ensure a strictly
well-defined initialization.
That would be awesome @jchiu if possible. Or I could do these changes as part of my PR and you could review those before I merge. Let me know what works for you.