3 ms·
To be fair, you can write a lot of idiomatic code without ever using packages that use `init()` [0]. (The most common usage is importing database drivers). I'
by gen220 6y ago
To be fair, you can write a lot of idiomatic code without ever using packages that use `init()` [0].
(The most common usage is importing database drivers).
I've written Go for a while, and it still feels a bit exotic, compared to its other control flow patterns.
Maybe "code smell" was the wrong word. But writing "surprising" logic in an `init()` function would definitely not be considered idiomatic.
[0]: https://golang.org/doc/effective_go.html#init https://golang.org/doc/effective_go.html#init
- tptacek 6y agoI guess I'd just say, when a Go programmer sees an "import _" in the README.md for a library in Github, they generally know exactly what's happening; "oh, you import this for side effects".
- gen220 6y agoYea that's fair. It's admittedly a more exotic than the traditional alternative: importing a module, initializing some struct, passing it to some other module's struct-initialization. Of course, now that I write it all out, and I imagine that I'd need to write all that just to give my gRPC service a load balancer... ok, I can accept the fancy hand-waves, give me the underscore import! :)