1 回答
TA贡献1839条经验 获得超15个赞
更改自:
value := reflect.New(query.structType)
for bindQuery.Scan(&value) {
到:
value := reflect.New(query.structType).Interface()
for bindQuery.Scan(value) {
有关完整的工作示例,请参见此处(粘贴在下面):
package main
import "reflect"
func Scan(d interface{}) {
v := reflect.ValueOf(d)
i := reflect.Indirect(v)
s := i.Type()
println(s.NumField()) // will print out 0, if you change Host to have 1 field, it prints out 1
}
func query(t reflect.Type) {
value := reflect.New(t).Interface()
Scan(value)
}
type Host struct{}
// type Host struct{int} // comment above line, uncomment this one, and println above will print 1
func main() {
var h Host
query(reflect.TypeOf(h))
}
这模拟了您的代码以及 clqr 库的作用(请参阅https://github.com/relops/cqlr/blob/master/cqlr.go#L85-L99和https://github.com/relops/cqlr/ blob/master/cqlr.go#L154-L160)。你基本上需要s := i.Type()成为TypeOf你的Host结构,所以如果你从 clqr 代码所做的事情向后工作,你可以推断出你需要传递给Scan调用的内容。并且鉴于您拥有的输入是 a reflect.Type,您可以推断出如何从中获得Type要传递到的正确类型的对象Scan。
- 1 回答
- 0 关注
- 165 浏览
添加回答
举报