-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (53 loc) · 1022 Bytes
/
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
package main
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
)
var DB *gorm.DB
type MySQLConfig struct {
UserName string
Password string
Host string
Port int
Name string
TimeOut string
}
func (m *MySQLConfig) DSN() string {
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local&timeout=%s",
m.UserName, m.Password, m.Host, m.Port, m.Name, m.TimeOut)
}
func main() {
config := MySQLConfig{
UserName: "",
Password: "",
Host: "",
Port: 3306,
Name: "",
TimeOut: "",
}
var err error
DB, err = gorm.Open(mysql.Open(config.DSN()), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
})
if err != nil {
panic(err)
}
people := &People{
Name: "rory",
Age: 24,
AddressList: AddressList{
{
Country: "China",
Province: "Guangdong",
City: "Guangzhou",
Detail: "xxx",
},
},
}
if err = DB.Model(&People{}).Save(&people).Error; err != nil {
log.Fatalln(err)
}
}