Go SQL扫描/值接口问题

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

Go SQL Scan/Value Interface Issue

问题

我正在尝试为我的自定义结构体添加一个用于自动转换的扫描/值接口。我已经成功在bool类型上实现了Value()和Scan()方法,但是当我尝试在一个包含gocql.UUID字段的结构体上实现时,无法使Scan()方法正常工作。

如果有任何建议,将不胜感激!

简短示例:

  1. type Uid struct {
  2. gocql.UUID
  3. }
  4. func (u *Uid) Scan(value interface{}) error {
  5. ...
  6. if sv, err := driver.String.ConvertValue(value); err == nil {
  7. if v, ok := sv.(string); ok { // <--- 这里无法工作
  8. parsedUUID, _ := gocql.ParseUUID(v)
  9. ...
  10. }
  11. }

完整代码:
https://play.golang.org/p/ndCZTJZ5rb

英文:

I am attempting to add a scan/value interface for automatic conversion for my custom structs. I was also able to implement both Value() and Scan() on a bool type, but when trying to implement it on something with a gocql.UUID field, I can't get the Scan() to work.

Any suggestions would be very much appreciated!

Short Example:

  1. type Uid struct {
  2. gocql.UUID
  3. }
  4. func (u *Uid) Scan(value interface{}) error {
  5. ...
  6. if sv, err := driver.String.ConvertValue(value); err == nil {
  7. if v, ok := sv.(string); ok { // <--- THIS DOESN'T WORK
  8. parsedUUID, _ := gocql.ParseUUID(v)
  9. ...
  10. }
  11. }

Full code:
https://play.golang.org/p/ndCZTJZ5rb

答案1

得分: 1

这个值已经被结构化为字节数组,所以解决方案如下:

  1. fmt.Sprintf("%s", sv)
英文:

The value was already structured as a byte array, so the solution ended up being:

  1. fmt.Sprintf("%s", sv)

huangapple
  • 本文由 发表于 2017年9月19日 11:38:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/46291389.html
匿名

发表评论

匿名网友

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

确定