Implicit initialization of lib/pq messes with other imports
anaulin opened this issue · 2 comments
anaulin commented
Hi there, thanks for the handy library!
Since I was already using github.com/lib/pq
in my app, when I added a dependency on github.com/antonlindstrom/pgstore
I got the following runtime error:
panic: sql: Register called twice for driver postgres
goroutine 1 [running]:
panic(0x420ec0, 0xc820208e30)
/usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:464 +0x3e6
database/sql.Register(0x5a78c0, 0x8, 0x9b2b58, 0x84dd30)
/usr/local/Cellar/go/1.6/libexec/src/database/sql/sql.go:45 +0x181
github.com/bolt-data/boltapp/vendor/github.com/lib/pq.init.1()
/Users/anaulin/src/github.com/bolt-data/boltapp/vendor/github.com/lib/pq/conn.go:44 +0x71
github.com/bolt-data/boltapp/vendor/github.com/lib/pq.init()
/Users/anaulin/src/github.com/bolt-data/boltapp/vendor/github.com/lib/pq/user_posix.go:24 +0x4b0
github.com/bolt-data/boltapp/db.init()
/Users/anaulin/src/github.com/bolt-data/boltapp/db/connection.go:27 +0x59
github.com/bolt-data/boltapp/handlers.init()
/Users/anaulin/src/github.com/bolt-data/boltapp/handlers/template_util.go:28 +0x5b
main.init()
I can work around this issue by removing my own import of github.com/lib/pq
but it feels a little messy, for a couple of reasons:
- It is no longer explicit in my code which Postgres driver is being used and where
- Some other dependency that also registers
lib/pq
will mess with things
elithrar commented
Agreed that it is undesirable behaviour.
It'd be a breaking change for existing users who only use this lib (and not Postgres elsewhere, which would be weird, but not unheard of). You can't conditionally import either.
TL;DR: Probably a change worth making.
antonlindstrom commented
I agree, this is something that I think that we should update and document.