What a fuck confusion of database/sql driver's import

3,605 views
Skip to first unread message

Liigo Zhuang

unread,
Jul 12, 2013, 10:26:50 PM7/12/13
to golang-nuts
When import "github.com/go-sql-driver/mysql", I got compile error:
"imported and not used: github.com/go-sql-driver/mysql"

And after removing this import, I got runtime error:
"sql: unknown driver "mysql" (forgotten import?)"

I do know how to make it happy, I just saying it's confusing and annoying, especially for beginners.

----------------------------------------------------------------------
package main

import "fmt"
import "database/sql"

func main() {
db, err := sql.Open("mysql", "liigo:zhu...@192.168.1.100");
if err != nil {
fmt.Println("connect fails:", err)
return
}
db.Close()
}
----------------------------------------------------------------------

--
by Liigo, http://blog.csdn.net/liigo/

Dave Cheney

unread,
Jul 12, 2013, 10:30:08 PM7/12/13
to Liigo Zhuang, golang-nuts
Use a side effect import

import _ "github.com/go-sql-driver/mysql"
> --
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Kyle Lemons

unread,
Jul 13, 2013, 1:10:16 AM7/13/13
to Liigo Zhuang, golang-nuts
Please don't employ vulgar language on this mailing list.


--

Carlos Castillo

unread,
Jul 13, 2013, 7:53:08 PM7/13/13
to golan...@googlegroups.com, Liigo Zhuang
To be fair, AFAIK nowhere in the database/sql documentation does it say to do this. Although to fling blame the other way, the README.md for github.com/go-sql-driver/mysql actually does say what to do (under Usage).

In the documentation of the image package, it shows in the package comment how to use a side-effect import to register the PNG driver, perhaps database/sql should do the same (unfortunately there are no built-in SQL drivers to use as example).


On Friday, July 12, 2013 7:30:08 PM UTC-7, Dave Cheney wrote:
Use a side effect import

import _ "github.com/go-sql-driver/mysql"

On Sat, Jul 13, 2013 at 12:26 PM, Liigo Zhuang <com....@gmail.com> wrote:
> When import "github.com/go-sql-driver/mysql", I got compile error:
> "imported and not used: github.com/go-sql-driver/mysql"
>
> And after removing this import, I got runtime error:
> "sql: unknown driver "mysql" (forgotten import?)"
>
> I do know how to make it happy, I just saying it's confusing and annoying,
> especially for beginners.
>
> ----------------------------------------------------------------------
> package main
>
> import "fmt"
> import "database/sql"
> import "github.com/go-sql-driver/mysql"
>
> func main() {
> db, err := sql.Open("mysql", "liigo:...@192.168.1.100");

Julien Schmidt

unread,
Jul 14, 2013, 11:43:39 AM7/14/13
to golan...@googlegroups.com
For beginners I would recommend this detailed tutorial for the database/sql package: https://github.com/VividCortex/go-database-sql-tutorial

Looking at your code I'm also pretty sure your DSN string is wrong, here are examples how it should look like: https://github.com/go-sql-driver/mysql#examples
The more technical and detailed specification can be found above.

On Saturday, July 13, 2013 4:26:50 AM UTC+2, Liigo Zhuang wrote:
When import "github.com/go-sql-driver/mysql", I got compile error:
"imported and not used: github.com/go-sql-driver/mysql"

And after removing this import, I got runtime error:
"sql: unknown driver "mysql" (forgotten import?)"

I do know how to make it happy, I just saying it's confusing and annoying, especially for beginners.

----------------------------------------------------------------------
package main

import "fmt"
import "database/sql"

func main() {
db, err := sql.Open("mysql", "liigo:...@192.168.1.100");

Andrew Gerrand

unread,
Jul 14, 2013, 6:48:47 PM7/14/13
to liigo, golang-nuts


On 13 Jul 2013 12:26, "Liigo Zhuang" <com....@gmail.com> wrote:
>
> When import "github.com/go-sql-driver/mysql", I got compile error:
> "imported and not used: github.com/go-sql-driver/mysql"
>
> And after removing this import, I got runtime error:
> "sql: unknown driver "mysql" (forgotten import?)"
>
> I do know how to make it happy, I just saying it's confusing and annoying, especially for beginners.

I submit that it is confusing and annoying *only* for beginners. Side-effect imports are a common Go idiom, and one that every Go programmer should understand.

Another example is the image package and the various image codec packages: by underscore-importing "image/png" you give the "image" package the ability to read PNG files.

Andrew 

David DENG

unread,
Jul 14, 2013, 7:36:03 PM7/14/13
to golan...@googlegroups.com, liigo
In document of package image, there are clear instructions about side-effect importing. I think package sql needs the same thing.

David

ps: @Liigo, using words like f*** is both rude and stupid, especially on the title.

Andrew Gerrand

unread,
Jul 14, 2013, 9:20:11 PM7/14/13
to David DENG, golang-nuts, liigo
On 15 July 2013 09:36, David DENG <david...@gmail.com> wrote:
In document of package image, there are clear instructions about side-effect importing. I think package sql needs the same thing.

I agree the docs need improvement. Filed http://golang.org/issue/5886

Andrew

Hans Stimer

unread,
Jul 14, 2013, 9:40:30 PM7/14/13
to David DENG, golan...@googlegroups.com, liigo



ps: @Liigo, using words like f*** is both rude and stupid, especially on the title.

The guy is frustrated, and he vented a little bit. No harm in that, as long as it doesn't become disruptive.


In this case, with the poor grammar, I find it endearing. Kind of like "All your base are belong to us", or "pwnd".


"What a fuck confusion" is one of the better ones I've heard lately. However, I haven't had many of those cases with Go, as compared to every other language I've used. I think the top "What a fuck confusion" goes to C++, or more consisely WFC C++. I think the WFC C++ meter hit 11, not to mention the ridiculous compile times, when people started metaprogramming with templates.

Dan Kortschak

unread,
Jul 14, 2013, 9:47:07 PM7/14/13
to Hans Stimer, David DENG, golan...@googlegroups.com, liigo
On Sun, 2013-07-14 at 18:40 -0700, Hans Stimer wrote:
> ps: @Liigo, using words like f*** is both rude and stupid, especially
> on the title.

But it was retained in the response? (not directed at Hans).

> The guy is frustrated, and he vented a little bit. No harm in that, as
> long as it doesn't become disruptive.
>
> In this case, with the poor grammar, I find it endearing. Kind of
> like "All your base are belong to us", or "pwnd".

Yes, it's pretty clear the OP does not have English as his first
language. People often swear beyond what is normally deemed appropriate
when speaking/writing in a foreign language.

He's got his answer though. So can we leave this.

Liigo Zhuang

unread,
Jul 15, 2013, 2:18:19 AM7/15/13
to Julien Schmidt, golang-nuts
I just complain, not ask for help.

I do known the way of "import _ xxxxx" BEFORE posting original email. Remember my last words in original email?

"I do know how to make it happy, I just saying it's confusing and annoying, especially for beginners."


2013/7/14 Julien Schmidt <g...@julienschmidt.com>
For beginners I would recommend this detailed tutorial for the database/sql package: https://github.com/VividCortex/go-database-sql-tutorial 

Looking at your code I'm also pretty sure your DSN string is wrong, here are examples how it should look like: https://github.com/go-sql-driver/mysql#examples
The more technical and detailed specification can be found above.

Yes, thanks. the dsn string is wrong. I do found it. It's easy to fix. This is out of topic.
 

On Saturday, July 13, 2013 4:26:50 AM UTC+2, Liigo Zhuang wrote:
When import "github.com/go-sql-driver/mysql", I got compile error:
"imported and not used: github.com/go-sql-driver/mysql"

And after removing this import, I got runtime error:
"sql: unknown driver "mysql" (forgotten import?)"

I do know how to make it happy, I just saying it's confusing and annoying, especially for beginners.

----------------------------------------------------------------------
package main

import "fmt"
import "database/sql"

func main() {
db, err := sql.Open("mysql", "liigo:...@192.168.1.100");
if err != nil {
fmt.Println("connect fails:", err)
return
}
db.Close()
}
----------------------------------------------------------------------

--
by Liigo, http://blog.csdn.net/liigo/

--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Liigo Zhuang

unread,
Jul 15, 2013, 2:45:16 AM7/15/13
to John, golang-nuts
2013/7/14 John <johns...@gmail.com>
Also, though you didn't ask about any of this, you can make this much nicer really easy.  Figured since I was looking I would throw in some pointers that
helped me out when I started with Go.
----------------------------------------------------------------------
package main

import (    // This way you dont have to say import, import,import.  You can do this with "var" and "const" too.
    "fmt"
    "database/sql"
    _ "github.com/go-sql-driver/mysql'  // I changed this to the side effect import.
)

func main() {
    db, err := sql.Open("mysql", "liigo:...@192.168.1.100")  // Don't need the ";"
    defer db.Close()  // Defer makes it so when your main exits db.Close() gets called.

You are wrong. If connect fails, db will be nil, and defered db.Close() will panic. I'm using the proper way.
 
    if err != nil {
        fmt.Println("connect fails:", err)  
        return
    }
}
----------------------------------------------------------------------

On Friday, July 12, 2013 7:26:50 PM UTC-7, Liigo Zhuang wrote:
When import "github.com/go-sql-driver/mysql", I got compile error:
"imported and not used: github.com/go-sql-driver/mysql"

And after removing this import, I got runtime error:
"sql: unknown driver "mysql" (forgotten import?)"

I do know how to make it happy, I just saying it's confusing and annoying, especially for beginners.

----------------------------------------------------------------------
package main

import "fmt"
import "database/sql"

func main() {
db, err := sql.Open("mysql", "liigo:...@192.168.1.100");
if err != nil {
fmt.Println("connect fails:", err)
return
}
db.Close()
}
----------------------------------------------------------------------

--
by Liigo, http://blog.csdn.net/liigo/

minux

unread,
Jul 15, 2013, 2:55:06 AM7/15/13
to Liigo Zhuang, John, golang-nuts
On Mon, Jul 15, 2013 at 2:45 PM, Liigo Zhuang <com....@gmail.com> wrote:
2013/7/14 John <johns...@gmail.com>
Also, though you didn't ask about any of this, you can make this much nicer really easy.  Figured since I was looking I would throw in some pointers that
helped me out when I started with Go.
----------------------------------------------------------------------
package main

import (    // This way you dont have to say import, import,import.  You can do this with "var" and "const" too.
    "fmt"
    "database/sql"
    _ "github.com/go-sql-driver/mysql'  // I changed this to the side effect import.
)

func main() {
    db, err := sql.Open("mysql", "liigo:...@192.168.1.100")  // Don't need the ";"
    defer db.Close()  // Defer makes it so when your main exits db.Close() gets called.

You are wrong. If connect fails, db will be nil, and defered db.Close() will panic. I'm using the proper way.
the defer statement won't panic, only when the deferred statement (db.Close() runs) could it
panic. so your code is still incorrect. and the general Go idiom is to compare err with nil
first, if it's not nil, prepare the err and return; after that defer resource.Close(). 

John

unread,
Jul 15, 2013, 3:30:42 AM7/15/13
to golan...@googlegroups.com, Liigo Zhuang, John
minux is right, I should have just had it after the error check.  Oversight on my part.

Aram Hăvărneanu

unread,
Jul 15, 2013, 7:14:25 AM7/15/13
to Liigo Zhuang, Julien Schmidt, golang-nuts
> I just complain, not ask for help.

Complain away then.

--
Aram Hăvărneanu

androi...@gmail.com

unread,
Mar 28, 2014, 5:52:26 AM3/28/14
to golan...@googlegroups.com, Liigo Zhuang
It worked for me...Thank you :)


On Saturday, July 13, 2013 8:00:08 AM UTC+5:30, Dave Cheney wrote:
Use a side effect import

import _ "github.com/go-sql-driver/mysql"

On Sat, Jul 13, 2013 at 12:26 PM, Liigo Zhuang <com....@gmail.com> wrote:
> When import "github.com/go-sql-driver/mysql", I got compile error:
> "imported and not used: github.com/go-sql-driver/mysql"
>
> And after removing this import, I got runtime error:
> "sql: unknown driver "mysql" (forgotten import?)"
>
> I do know how to make it happy, I just saying it's confusing and annoying,
> especially for beginners.
>
> ----------------------------------------------------------------------
> package main
>
> import "fmt"
> import "database/sql"
> import "github.com/go-sql-driver/mysql"
>
> func main() {
> db, err := sql.Open("mysql", "liigo:...@192.168.1.100");
Reply all
Reply to author
Forward
0 new messages