复习一下
对象
package main
import(
"fmt"
)
type User struct{
uid int
name string
}
func main(){
var user User
fmt.Println(user)
}
协程
package main
import(
"fmt"
)
func test(){
fmt.Println("test func")
}
func main(){
go test()
fmt.Println("main func")
}
当你像我一样的写法,只会输出main func
(go test()
启动线程没有问题,问题是你的主线程执行完了后go test()
其实协程还没有启动主线程就停止了,所以需要主线程来协助)
package main
import(
"fmt"
"time"
)
func test(){
fmt.Println("test func")
}
func main(){
go test()
fmt.Println("main func")
time.Sleep(time.Second*1)
}
mysql链接
驱动为:github.com/go-sql-driver/mysql
go get github.com/go-sql-driver/mysql
go get github.com/jmoiron/sqlx
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
go env -w GOPROXY=https://goproxy.cn,direct
初始化数据库连接
package main
import(
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
func main(){
DB, _ := sqlx.Open("mysql","root:YourMySQLPassWord@tcp(127.0.0.1:3306)/yourDataBaseName")
//设置数据库最大连接数
DB.SetConnMaxLifetime(100)
//设置上数据库最大闲置连接数
DB.SetMaxIdleConns(10)
//验证连接
if err := DB.Ping(); err != nil {
fmt.Println("open database fail")
return
}
fmt.Println("connnect success")
}
查询操作
不知道结构怎么定义的可查看mysql的对应数据库中 desc 表名;
来查看
sqlx.Open(…).Query
package main
import(
"fmt"
"encoding/json"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
//struct序列化tag
type User struct{
Uid int `json:"uid"`
Name string `json:"name"`
Pwd string `json:"pwd"`
}
//数据库指针
var db *sqlx.DB
func init(){
// database,err := sqlx.Open("mysql","root:qwertyuiop1@tcp(127.0.0.1)/learn_test")
// if err != nil {
// fmt.Println("open mysql failed,", err)
// }
// db = database
}
func main(){
var user User
DB, _ := sqlx.Open("mysql","root:password@tcp(127.0.0.1)/database")
//设置数据库最大连接数
DB.SetConnMaxLifetime(100)
//设置上数据库最大闲置连接数
DB.SetMaxIdleConns(10)
//验证连接
if err := DB.Ping(); err != nil {
fmt.Println("open database fail")
return
}
fmt.Println("connnect success")
rows, e := DB.Query("select * from users")
if e != nil {
fmt.Println("query incur error")
}
for rows.Next(){
e := rows.Scan(user.Uid, user.Name, user.Pwd)
if e != nil{
fmt.Println(json.Marshal(user))
}
}
rows.Close()
}
sqlx.Open(…).Select()
package main
import(
"fmt"
// "encoding/json"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
//struct序列化tag
type User struct{
Uid int `db:"uid"`
Name string `db:"name"`
Pwd string `db:"pwd"`
}
//数据库指针
var db *sqlx.DB
func init(){
database,err := sqlx.Open("mysql","root:password@tcp(127.0.0.1)/database")
if err != nil {
fmt.Println("open mysql failed,", err)
}
db = database
}
func main(){
var user []User
//sql := "select * from users"
sql := "select * from users"
err := db.Select(&user, sql)
if err != nil {
fmt.Println("exec failed, ", err)
return
}
fmt.Println("select succ:", user)
}
添加
package main
import(
"fmt"
// "encoding/json"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
//struct序列化tag
type User struct{
Uid int `json:"uid"`
Name string `json:"name"`
Pwd string `json:"pwd"`
}
//数据库指针
var db *sqlx.DB
func init(){
database,err := sqlx.Open("mysql","root:password@tcp(127.0.0.1)/database")
if err != nil {
fmt.Println("open mysql failed,", err)
}
db = database
}
func main(){
sql := "insert into users(name,pwd) values(?,?)"
value := [2]string{"wanwu","112233qw"}
//执行sql语句
r,err := db.Exec(sql,value[0],value[1])
if err != nil {
fmt.Println("exec failed",err)
return
}
//查询最后一天用户ID,判断是否插入成功
id,err := r.LastInsertId()
if err != nil {
fmt.Println("exec failed2",err)
return
}
fmt.Println("insert succ",id)
}
事务
事务一般都是在
insert
的时候使用
sqlx.DB.Begin() 开始事务
sqlx.DB.Commit() 提交事务
sqlx.DB.Rollback() 回滚事务
conn,err := db.Begin()
if err != nil{
fmt.Println("begin failed :", err)
return
}
//执行插入语句
r, err := conn.Exec("insert into users(name, pwd)values(?, ?)", "value1", "value2")
if err != nil {
fmt.Println("exec failed, ", err)
conn.Rollback() //出现异常,进行回滚操作
return
}
id, err := r.LastInsertId()
if err != nil {
fmt.Println("exec failed, ", err)
conn.Rollback()
return
}
fmt.Println("insert succ:", id)
conn.Commit() //提交
修改