英文:
Go SQL Scan/Value Interface Issue
问题
我正在尝试为我的自定义结构体添加一个用于自动转换的扫描/值接口。我已经成功在bool类型上实现了Value()和Scan()方法,但是当我尝试在一个包含gocql.UUID字段的结构体上实现时,无法使Scan()方法正常工作。
如果有任何建议,将不胜感激!
简短示例:
type Uid struct {
gocql.UUID
}
func (u *Uid) Scan(value interface{}) error {
...
if sv, err := driver.String.ConvertValue(value); err == nil {
if v, ok := sv.(string); ok { // <--- 这里无法工作
parsedUUID, _ := gocql.ParseUUID(v)
...
}
}
完整代码:
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:
type Uid struct {
gocql.UUID
}
func (u *Uid) Scan(value interface{}) error {
...
if sv, err := driver.String.ConvertValue(value); err == nil {
if v, ok := sv.(string); ok { // <--- THIS DOESN'T WORK
parsedUUID, _ := gocql.ParseUUID(v)
...
}
}
Full code:
https://play.golang.org/p/ndCZTJZ5rb
答案1
得分: 1
这个值已经被结构化为字节数组,所以解决方案如下:
fmt.Sprintf("%s", sv)
英文:
The value was already structured as a byte array, so the solution ended up being:
fmt.Sprintf("%s", sv)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论