-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (54 loc) · 1.24 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"context"
"database/sql"
"fmt"
"log"
// 数据库驱动。下划线为命名,因为不会直接将驱动拿来写代码,所以命名为下划线
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
const (
user = "root"
password = "mysql"
database = "go_db"
)
/**
* docker run --name go-web -e MYSQL_ROOT_PASSWORD=mysql -p 3306:3306 -d mysql
* docker exec -it go-web bash
*/
func main() {
connectString := fmt.Sprintf("%s:%s@/%s", user, password, database)
var _error error
// Open 的第一个参数名为数据库驱动的名称
db, _error = sql.Open("mysql", connectString)
if _error != nil {
log.Fatalln(_error.Error())
return
}
_context := context.Background()
// PingContext 用来验证与数据库的连接是否仍然有效
_error = db.PingContext(_context)
if _error != nil {
log.Fatalln(_error.Error())
return
}
fmt.Println("Connected!")
// 查询复数条数据
// list, _error := getMany(1)
// 查询一条数据
user, _ := getOne(2)
user.name = "shamare"
// _error = user.Update()
user03 := customUser{
id: 3,
name: "suzuran",
}
_error = user03.Insert()
if _error != nil {
log.Fatalln(_error.Error())
return
}
resutl, _ := getOne(3)
fmt.Println(resutl)
}