英文:
Inserting custom time, Scanner and Valuer implemented — but still errs
问题
我有一个自定义的时间格式,是通过一些自定义的解组得到的:
type customTime struct {
time.Time
}
我已经在这个 customTime
上实现了 Scanner
和 Valuer
接口,代码如下:
func (ct *customTime) Scan(value interface{}) error {
ct.Time = value.(time.Time)
return nil
}
func (ct *customTime) Value() (driver.Value, error) {
return ct.Time, nil
}
但是当我尝试插入时仍然出错:
sql: converting Exec argument $3 type: unsupported type main.customTime, a struct
我漏掉了什么?
英文:
I have a custom time format that is the result of some custom unmarshalling:
type customTime struct {
time.Time
}
I have implemented the Scanner
and Valuer
interface on this customTime
like so:
func (ct *customTime) Scan(value interface{}) error {
ct.Time = value.(time.Time)
return nil
}
func (ct *customTime) Value() (driver.Value, error) {
return ct.Time, nil
}
But it still errs when I try to do the insert:
> sql: converting Exec argument $3 type: unsupported type main.customTime, a struct
What am I missing?
答案1
得分: 6
找到解决方案,Scanner
和 Valuer
应该在实际值上实现,而不是在 customTime
的指针上实现。
func (ct customTime) Scan(value interface{}) error {
ct.Time = value.(time.Time)
return nil
}
func (ct customTime) Value() (driver.Value, error) {
return ct.Time, nil
}
英文:
Found the solution, Scanner
and Valuer
should be implemented on the actual value and not a pointer to the customTime
func (ct customTime) Scan(value interface{}) error {
ct.Time = value.(time.Time)
return nil
}
func (ct customTime) Value() (driver.Value, error) {
return ct.Time, nil
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论