使用GoLang的mysql连接失败

I am trying to connect the MySql DB using Go Language and gives me following error.

sql: unknown driver "mysql" (forgotten import?)

My Code

package main

    import (
        "database/sql"
        "fmt"
    )

func main() {
    db, err := sql.Open("mysql", "astaxie:astaxie@/test?charset=utf8")
    checkErr(err);
    err=db.Ping();
}

Also when I import

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

I am getting error of

imported and not used

Try it again, but look for my NOTEs:

package main

import (
    "database/sql"

    _ "github.com/go-sql-driver/mysql"
)
// NOTE - I removed the import for "fmt" because it was unused.

func main() {
    db, err := sql.Open("mysql", "astaxie:astaxie@/test?charset=utf8")
    checkErr(err);
    err=db.Ping();
    // NOTE - the above line will trigger an error because err is unused.
}

I added the import for the MySQL driver and removed "fmt" because it was unused. This may be the cause of your "imported and not used" error.

For others coming to this page as a result of the error sql: unknown driver "mysql" (forgotten import?), the database/sql package must be used in conjunction with a database driver. That means in addition to importing the database/sql package, you need to import a database driver.

For example, for mysql, you could use the package go-sql-driver. Typically, you import this package using the underscore _ notation, meaning it is imported for its side effects only:

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

You can read more about this and find a list of SQL drivers below:

Try to recheck the location of the packages. I made such a mistake when I manually added this package to the project. It is best to clean GOROOT and GOPATH from this package and reinstall/reconnect it as indicated in the source: https://github.com/go-sql-driver/mysql