3 ms·
"Typically the main() function would call the various init functions like InitMongoService" Why muddy your main() when init() exists? See: http://golang.org/r
by mutatio 12y ago
"Typically the main() function would call the various init functions like InitMongoService"
Why muddy your main() when init() exists?
See: http://golang.org/ref/spec#Package_initialization http://golang.org/ref/spec#Package_initialization
- sagichmal 12y agoAnd why use InitXxx at all, when you can create an initialized, locally-scoped MongoService with a constructor, and pass it to the things that need it?
- mutatio 12y agoI was referring to Go's magic func init() Putting that in your files, let's say: db.go func init() => handle global DB's... templates.go func init() => handle HTML templates... settings.go func init() => load some settings... All fired automatically prior to your applications main() entry point WITHOUT needing to pollute main() with initDB(); initTemplates(); initSettings() etc. etc.
- nostrademons 12y agoThey're trying to pass the objects created at startup into other objects, so that dependencies are explicit and can be mocked out in unit tests. You can't do this with init(), which always takes no arguments and returns no values.
- sagichmal 12y ago> I was referring to Go's magic func init() I know you were; I'm arguing that components shouldn't need an explicit initialization step, but rather they should be initialized as part of their `NewFoo` constructor.