21
import (
    _ "github.com/lib/pq"
    _ "image/png"
    ...
)

In effective go it says that these kinds of imports mean side effect. I've read several SO answers but none explain what is an import side effect. Could someone elaborate the term import side effect?

1 Answer 1

25

When they say 'import side effects' they are essentially referring to code/features that are used statically. Meaning just the import of the package will cause some code to execute on app start putting my system in a state different than it would be without having imported that package (like code in an init() which in their example registers handlers, it could also lay down config files, modify resource on disc, ect). The effective go tutorial is explaining this simply to illustrate reasons why a developer might want to do a blank import ie; import _ "somepackageImNotUsingReally"

EDIT: to add additional context when I said init() I was referring to this method; https://golang.org/doc/effective_go.html#init - any imported packages will have their init methods called prior to main being called. Whatever is in the init() is a side effect. I don't think there can be any others because things like constants will be at the package scope, not the global scope so it wouldn't redefine constants or anything like that.

EDIT2: as pointed out in comments and explained in the init link above " is called after all the variable declarations in the package have evaluated their initializers" meaning code like PackageScopeVar := unexportedInitializerThatWritesToDisc() will run and could have side effects.

12
  • Still doesn't explain how this lib/pq init() works. It only calls sql.Register("postgres", &drv{}). How does all the other codes effect?
    – Thellimist
    Sep 8, 2015 at 19:22
  • 2
    @Entei yes it does... Firstly, I explained what an import side effect is, not the side effect of that specific import. If you'd like to know the specific side effect of that then look at the docs for sql.Register golang.org/pkg/database/sql/#Register ... if you call that method again in some other scope it will panic. That effects all the code. Sep 8, 2015 at 19:34
  • 2
    "It only calls sql.Register("postgres", &drv{})" is the desired side effect: The packages registers itself to be known in package sql.
    – Volker
    Sep 8, 2015 at 19:34
  • 1
    @evanmcdonnal, don't forget, in addition to init a package can do something like var foo = someOtherFunction() as a package variable which will call the function (and have side effects) before init even runs. E.g. var _ = os.Remove("somefile")
    – Dave C
    Sep 8, 2015 at 19:36
  • @DaveC yeah, I suppose that should be included in the answer though reading the 1 paragraph blurb on init explains the order in which things happen. Sep 8, 2015 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.