lint: a blank import should be only in a main or test package, or have a comment justifying it

1,843 views
Skip to first unread message

Yann Salaün

unread,
Aug 15, 2015, 8:36:41 AM8/15/15
to golan...@googlegroups.com
Hi,

I ran lint on some of my code today and got this message. In short, the code is a webapp with a `models` package where I put all the sql code. This package needs a blank import to load the sql driver.

I was wondering what could be the motivations behind this message. Why is this a bad practice to have a blank import in a package other that `main`?

Have a good day

Yann

David Symonds

unread,
Aug 15, 2015, 6:40:02 PM8/15/15
to Yann Salaün, golang-nuts
Blank imports are usually only used for their side effects (otherwise
you'd be using the package by name). Since the side effects generally
affect the whole program (e.g. registering an SQL driver), they should
be done as high as possible in the dependency tree (generally package
main) so that it doesn't preclude the lower packages from being
re-used with a different side effect package. In your case, your
models package might likely be usable in other situations (e.g. with a
different driver, or just for tests) and so it shouldn't force a
dependency on a specific driver.
Reply all
Reply to author
Forward
0 new messages