type User struct { ID uint// Standard field for the primary key Name string// A regular string field Email *string// A pointer to a string, allowing for null values Age uint8// An unsigned 8-bit integer Birthday *time.Time // A pointer to time.Time, can be null MemberNumber sql.NullString // Uses sql.NullString to handle nullable strings ActivatedAt sql.NullTime // Uses sql.NullTime for nullable time fields CreatedAt time.Time // Automatically managed by GORM for creation time UpdatedAt time.Time // Automatically managed by GORM for update time }
如果您希望保存 UNIX(毫秒/纳秒)秒而不是时间,则只需将字段的数据类型从
time.Time
更改为
int
即可
type User struct { CreatedAt time.Time // Set to current time if it is zero on creating UpdatedAt int// Set to current unix seconds on updating or if it is zero on creating Updated int64`gorm:"autoUpdateTime:nano"`// Use unix nano seconds as updating time Updated int64`gorm:"autoUpdateTime:milli"`// Use unix milli seconds as updating time Created int64`gorm:"autoCreateTime"`// Use unix seconds as creating time }