type User struct { gorm.Model Name string Age sql.NullInt64 Birthday *time.Time Email string`gorm:"type:varchar(100);unique_index"` Role string`gorm:"size:255"`// 设置字段大小为255 MemberNumber *string`gorm:"unique;not null"`// 设置会员号(member number)唯一并且不为空 Num int`gorm:"AUTO_INCREMENT"`// 设置 num 为自增类型 Address string`gorm:"index:addr"`// 给address字段创建名为addr的索引 IgnoreMe int`gorm:"-"`// 忽略本字段 }
// sql.Scanner type Scanner interface { // Scan assigns a value from a database driver. // // The src value will be of one of the following types: // // int64 // float64 // bool // []byte // string // time.Time // nil - for NULL values // // An error should be returned if the value cannot be stored // without loss of information. // // Reference types such as []byte are only valid until the next call to Scan // and should not be retained. Their underlying memory is owned by the driver. // If retention is necessary, copy their values before the next call to Scan. Scan(src interface{}) error }
// driver.Valuer type Valuer interface { // Value returns a driver Value. // Value must not panic. Value() (Value, error) }
我们可以发现 Valuer 用于保存数据的时候,Scaner 用于数据从数据库映射到 model 的时候