58 Star 602 Fork 278

ShirDon-廖显东 / Go Web编程实战派源码

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
3-2-model-user.go 1007 Bytes
一键复制 编辑 原始数据 按行查看 历史
ShirDon-廖显东 提交于 2021-01-19 21:02 . commit
//++++++++++++++++++++++++++++++++++++++++
// 《Go Web编程实战派从入门到精通》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goWebActualCombat
// 仓库地址:https://github.com/shirdonl/goWebActualCombat
//++++++++++++++++++++++++++++++++++++++++
package model
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
var DB *sql.DB
type User struct {
Uid int
Name string
Phone string
}
//初始化数据库连接
func init() {
DB, _ = sql.Open("mysql",
"root:123456@tcp(127.0.0.1:3306)/chapter3")
}
//获取用户信息
func GetUser(uid int) (u User) {
// 非常重要:确保QueryRow之后调用Scan方法,否则持有的数据库链接不会被释放
err := DB.QueryRow("select uid,name,phone from `user` where uid=?", uid).Scan(&u.Uid, &u.Name, &u.Phone)
if err != nil {
fmt.Printf("scan failed, err:%v\n", err)
return
}
return u
}
Go
1
https://gitee.com/shirdonl/goWebActualCombat.git
git@gitee.com:shirdonl/goWebActualCombat.git
shirdonl
goWebActualCombat
Go Web编程实战派源码
bcfc89f778fc

搜索帮助

53164aa7 5694891 3bd8fe86 5694891