插入自定义时间、扫描器和值处理程序已实现,但仍存在错误。

huangapple go评论77阅读模式
英文:

Inserting custom time, Scanner and Valuer implemented — but still errs

问题

我有一个自定义的时间格式,是通过一些自定义的解组得到的:

type customTime struct {
    time.Time
}

我已经在这个 customTime 上实现了 ScannerValuer 接口,代码如下:

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

找到解决方案,ScannerValuer 应该在实际值上实现,而不是在 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
}

huangapple
  • 本文由 发表于 2017年6月20日 03:39:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/44638610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定