-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorm.go
66 lines (56 loc) · 1.13 KB
/
gorm.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
package main
import (
"fmt"
"log"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type Product struct {
gorm.Model
Code string
Price uint
}
func mainGorm() {
USER := "root"
PASS := ""
HOST := "localhost"
PORT := "3306"
DB_NAME := "go_videos"
LINK := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", USER, PASS, HOST, PORT, DB_NAME)
// connection
db, err := gorm.Open(mysql.Open(LINK), &gorm.Config{})
if err != nil {
panic("unable to connect")
}
// migrate schema
db.AutoMigrate(&Product{})
// create
db.Create(&Product{
Code: "A12",
Price: 99000,
})
db.Create(&Product{
Code: "B98",
Price: 150000,
})
// read
var product1 Product
var product2 Product
db.First(&product1, 1) // 1 is primary key
db.First(&product2, "code = ?", "B98") // find with code = B98
log.Println(product1)
log.Println(product2)
// update
db.Model(&product2).Update("Price", 200000)
db.Model(&product1).Updates(Product{
Price: 300000,
Code: "C55",
})
// or
db.Model(&product2).Updates(map[string]interface{}{
"Price": 300000,
"Code": "C54",
})
// delete
db.Delete(&product2, 2)
}