當下載go語言資料庫驅動時出現如下錯誤:
E:\Go\src\beegodemo>go get github.com/go-sql-driver/mysql
can't load package: package github.com/go-sql-driver/mysql: no Go files in E:\Go\src\github.com\go-sql-driv
er\mysql
我們去https://github.com/go-sql-driver/mysql查看問題:

然后重新下載

下載完成后,自動生成檔案:

下載驅動成功!
測驗代碼:
import (
"database/sql"
"github.com/astaxie/beego"
_"github.com/go-sql-driver/mysql"
)
type MysqlController struct {
beego.Controller
}
//1.打開資料庫
//2.操作資料庫
//3.管理資料庫
func (this *MysqlController)ShowMysql() {
//1.打開資料庫
//1.驅動名稱
//2.連接字串 mysql -uroot -p123456
conn,err:=sql.Open("mysql","root:123456@tcp(127.0.0.1:3306)/class1?charset=utf8")
if err!=nil{
beego.Info("連接錯誤",err)
return
}
//3.關閉資料庫
defer conn.Close()
//2操作資料庫
_,err=conn.Exec("create table userInfo(id int,name varchar(11)) ")
if err !=nil{
beego.Info("創建表錯誤",err)
return
}
this.Ctx.WriteString("創建表成功!")
}


uj5u.com熱心網友回復:
確保mysql 的包 在gopath里面 ,然后包含_"mysql"轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/33145.html
標籤:go語言
