Created tag will automatically inserted current time when you insert one record. To use this feature, you should use created tag at the related field.The field type could be time.Time, int, int64 and etc.
type User struct {
Id int64
Name string
CreatedAt time.Time `xorm:"created"`
}
Or
type JsonTime time.Time
func (j JsonTime) MarshalJSON() ([]byte, error) {
return []byte(`"`+time.Time(j).Format("2006-01-02 15:04:05")+`"`), nil
}
type User struct {
Id int64
Name string
CreatedAt JsonTime `xorm:"created"`
}
Or
type User struct {
Id int64
Name string
CreatedAt int64 `xorm:"created"`
}
When Insert()
or InsertOne()
be called, user.CreatedAt
will be filled with time.Now()
or time.Now().Unix()
. For example,
var user User
engine.Insert(&user)
// INSERT user (created...) VALUES (?...)