You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
925 B

2 years ago
package gormExample
import (
"fmt"
"gorm.io/gorm"
"log"
"time"
)
type Post struct{ gorm.Model }
type Category struct{ gorm.Model }
type PostCategory struct{ gorm.Model }
type Box struct{ gorm.Model }
func (Box) TableName() string {
return "my_box"
}
type TypeMap struct {
gorm.Model
FInt int
FUInt uint
FFloat32 float32
FFloat64 float64
FString string
FTime time.Time
FByteSlice []byte
FIntP *int
FUIntP *uint
FFloat32P *float32
FFloat64P *float64
FStringP *string
FTimeP *time.Time
}
func Migrate() {
if err := DB.Debug().AutoMigrate(&TypeMap{}, &Post{}, &Category{}, &PostCategory{}, &Box{}); err != nil {
log.Fatal(err)
}
}
func PointerDiff() {
// 模型的零值
typeMap := &TypeMap{}
fmt.Printf("%+v\n", typeMap)
fmt.Println("=================================")
// 查询数据NULL对应的值
DB.First(typeMap, 1)
fmt.Printf("%+v\n", typeMap)
}