E: \ Go \ SRC \ beegodemo> Go get github.com/go-sql-driver/mysql
Can 't load package: a package github.com/go-sql-driver/mysql: no Go files in E:\Go\src\github.com \ Go - SQL - driv
Er \ mysql
We go to https://github.com/go-sql-driver/mysql to check the problem:
And then to download
After the download is complete, automatically generated file:
Download the driver successfully!
The test code:
Import (
"Database/SQL
""Github.com/astaxie/beego
"_ "github.com/go-sql-driver/mysql
")
Type MysqlController struct {
Beego. Controller
}
//1. Open the database
//2. Database operations
//3. Management database
Func (this * MysqlController) ShowMysql () {
//1. Open the database
//1. The driver name
//2. The connection string mysql - uroot - p123456
Conn, err:=SQL. Open (" mysql ", "root: 123456 @ TCP/class1 (127.0.0.1:3306)? Charset=utf8 ")
If err! Nil={
Beego. Info (" connection error ", err)
Return
}
//3. Close the database
Defer conn. Close ()
//2 operation database
_, err=conn. Exec (" create table the userInfo (int id, name varchar (11)) ")
if err ! Nil={
Beego. Info (" create table error ", err)
Return
}
This. Ctx. WriteString (" create table success!" )
}
CodePudding user response:
Ensure that mysql package in gopath and contains _ "mysql"