
我这样自定义了时间类型,但是添加进数据库时,时间字段会为空
@shiguanghuxian
`// Value insert timestamp into mysql need this function.
func (ts TimeStamp) Value() (driver.Value, error) {
var zeroTime time.Time
var ti = time.Time(ts)
if ti.UnixNano() == zeroTime.UnixNano() {
return nil, nil
}
return ti, nil
}
// Scan valueof time.Time
func (ts *TimeStamp) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*ts = TimeStamp(value)
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}`
Thank you. The problem has been settled
@hellojukay
Most helpful comment
@shiguanghuxian
`// Value insert timestamp into mysql need this function.
func (ts TimeStamp) Value() (driver.Value, error) {
var zeroTime time.Time
var ti = time.Time(ts)
if ti.UnixNano() == zeroTime.UnixNano() {
return nil, nil
}
return ti, nil
}
// Scan valueof time.Time
func (ts *TimeStamp) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*ts = TimeStamp(value)
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}`