安装mysql驱动,在命令行中运行:

go get -u github.com/go-sql-driver/mysql

导入包:

package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	db, err := sql.Open("mysql", "root:root@tcp(localhost:3306)/test?charset=utf8")
	if err != nil {
		panic(err.Error())
	}
	fmt.Println(db)
}

使用示例:

https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/05.2.md