可以考虑的时间。在golang中的时间
发布时间:2020-12-16 19:19:51 所属栏目:大数据 来源:网络整理
导读:我有一个结构,我打算用数据库记录填充,其中一个日期时间列可以为空: type Reminder struct { Id int CreatedAt time.Time RemindedAt *time.Time SenderId int ReceiverId int} 由于指针可以为nil,因此我将RemindedAt设为指针,但这将要求代码知道At变量
我有一个结构,我打算用数据库记录填充,其中一个日期时间列可以为空:
type Reminder struct { Id int CreatedAt time.Time RemindedAt *time.Time SenderId int ReceiverId int } 由于指针可以为nil,因此我将RemindedAt设为指针,但这将要求代码知道At变量之间的差异。有更优雅的方式来处理这个问题吗?
你可以使用pq.NullTime。
从lib/pq开始,在github上: type NullTime struct { Time time.Time Valid bool // Valid is true if Time is not NULL } // Scan implements the Scanner interface. func (nt *NullTime) Scan(value interface{}) error { nt.Time,nt.Valid = value.(time.Time) return nil } // Value implements the driver Valuer interface. func (nt NullTime) Value() (driver.Value,error) { if !nt.Valid { return nil,nil } return nt.Time,nil } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |