-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (40 loc) · 1.02 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
package main
import (
"encoding/json"
"fmt"
"github.com/raaaaaaaay86/gorm-custom-datatype/model"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func main() {
db := CreateMySQLGormConnection()
// Create
item := model.Item{
Name: "Book",
Owner: []string{"Mary", "Jane"},
}
db.Create(&item)
// Query
queryItem := model.Item{}
db.Where("id = ?", item.ID).First(&queryItem)
marshaled, _ := json.MarshalIndent(queryItem, "", "\t")
fmt.Println(string(marshaled))
}
func CreateMySQLGormConnection() *gorm.DB {
db, err := gorm.Open(
mysql.New(mysql.Config{
DSN: "root:changeme@tcp(localhost:3307)/property?charset=utf8&parseTime=true&loc=Local",
DefaultStringSize: 256,
DisableDatetimePrecision: true,
DontSupportRenameIndex: true,
DontSupportRenameColumn: true,
SkipInitializeWithVersion: false,
}),
&gorm.Config{},
)
if err != nil {
panic(fmt.Errorf("gorm initialization: MySQL connection failed: %s", err))
}
// db.AutoMigrate(&model.Item{})
return db
}