package models import ( "database/sql/driver" "fmt" "time" ) type JSONTime time.Time func (t JSONTime) MarshalJSON() ([]byte, error) { ti := time.Time(t) tune := ti.Format(`"2006-01-02 15:04:05"`) return []byte(tune), nil } // Value insert timestamp into mysql need this function. func (t JSONTime) Value() (driver.Value, error) { var zeroTime time.Time ti := time.Time(t) if ti.UnixNano() == zeroTime.UnixNano() { return nil, nil } return ti, nil } // Scan ValueOf time.Time func (t *JSONTime) Scan(v interface{}) error { ti, ok := v.(time.Time) // NOT directly assertion v.(JSONTime) if ok { *t = JSONTime(ti) return nil } return fmt.Errorf("can not convert %v to timestamp", v) }