1、安装MysqL的库
这里推荐使用myMysqL
https://github.com/ziutek/myMysqL
WIN安装方法:
在Cmd下执行 3句
go get github.com/ziutek/myMysqL/thrsafe go get github.com/ziutek/myMysqL/autorc go get github.com/ziutek/myMysqL/godrv
2、使用方法
import( "fmt" "github.com/ziutek/myMysqL/MysqL" _ "github.com/ziutek/myMysqL/native" ) func main() { db := MysqL.New("tcp","","连接地址","数据库用户","密码","数据库名" err := db.Connect() if err != nil{ fmt.Println(err); //连接失败 } rows,res,err := db.Query("select * from 表") if err == nil{ fmt.Println(rows[0].Str(0)) //字符串显示第一个字段内容 name := res.Map("字段名") //查询字段名返回序号 fmt.Println(rows[0].Str(name)) } }原文链接:https://www.f2er.com/go/191247.html