我创建了这样的表:CREATE TABLE MyTable( id uuid, Test BOOLEAN NOT NULL, end_date TIMESTAMP NULL DEFAULT NULL, PRIMARY KEY (id));我的结构type Issue struct { ID uuid.UUID Test bool EndDate time.Time `db:"due_date"`}现在的情况是有一些日期没有在实时数据库中,所以现在我查询得到我得到这个错误EndDateall dataERROR: sql: Scan error on column index 11, name "i.end_date": unsupported Scan, storing driver.Value type <nil> into type *time.Time我不知道问题在哪里。更新如果我使用sql。NullTime,然后我做了一个像这样的反击模式return &model.Issue{ AssetOwnerID: id, DueDate : time.Now().UTC().Truncate(time.Second) } 我收到此错误 Cannot use 'time.Now().UTC().Truncate(time.Second)' (type Time) as type sql.NullTime
1 回答
www说
TA贡献1775条经验 获得超8个赞
您可以使用 sql。NullTime 类型,例如:
import (
"database/sql"
)
type Issue struct {
ID uuid.UUID
Test bool
EndDate sql.NullTime `db:"due_date"`
}
然后,您可以使用以下示例:
读取操作:
if i.EndDate.Valid {
fmt.Println(i.EndDate.Time.Unix())
} else {
fmt.Println("nil endDate")
}
写入操作:
i.EndTime.Valid = true
i.EndTime.Time = time.Unix(iEndTime, 0)
更新:
您可以将结构创建为:
return &model.Issue{
AssetOwnerID: id,
DueDate: sql.NullTime{
Time: time.Now().UTC().Truncate(time.Second),
Valid: true,
}
- 1 回答
- 0 关注
- 257 浏览
添加回答
举报
0/150
提交
取消
