Ai
1 Star 0 Fork 3

源码大数据(codebigdata)/goWebActualCombat

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
3-2-model-user.go 1007 Bytes
Copy Edit Raw Blame History
ShirDon-廖显东 authored 2021-01-19 21:02 +08:00 . 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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/codebigdata/goWebActualCombat.git
git@gitee.com:codebigdata/goWebActualCombat.git
codebigdata
goWebActualCombat
goWebActualCombat
b84124f40e18

Search