question about the "database/sql" package.

100 views
Skip to first unread message

sina safari

unread,
Jan 15, 2022, 5:20:20 PM1/15/22
to golang-nuts
Hi, I have a question about the "database/sql" package.
Why doesn't the "Open" function take the "driver.Driver" interface instead of the driver name?
like this:
func Open(driveri driver.Driver, dataSourceName string) (*DB, error)

what's the reason?

Axel Wagner

unread,
Jan 15, 2022, 5:33:28 PM1/15/22
to sina safari, golang-nuts
There is an argument to be made that this allows code using `database/sql` to take the arguments to `Open` directly from an environment variable/config file and pass them on, to support multiple DBMS. But really, it would have probably been a better design to directly take the Driver, yes. However, this was a decision made a long time ago and we can't really change it now.

Note that there is `sql.OpenDB`, which is pretty much doing what you expect.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9fad2844-253e-436c-9495-2f44394cc1b0n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages