--
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");
When import "github.com/go-sql-driver/mysql", I got compile error: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.----------------------------------------------------------------------
db, err := sql.Open("mysql", "liigo:...@192.168.1.100");
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
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.
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: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.----------------------------------------------------------------------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.
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 thathelped me out when I started with Go.----------------------------------------------------------------------
package mainimport ( // 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.
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: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.----------------------------------------------------------------------
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/
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 thathelped me out when I started with Go.----------------------------------------------------------------------
package mainimport ( // 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.
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");