1 回答

TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊
更改自:
value := reflect.New(query.structType)
for bindQuery.Scan(&value) {
到:
value := reflect.New(query.structType).Interface()
for bindQuery.Scan(value) {
有關(guān)完整的工作示例,請(qǐng)參見此處(粘貼在下面):
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 庫的作用(請(qǐng)參閱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結(jié)構(gòu),所以如果你從 clqr 代碼所做的事情向后工作,你可以推斷出你需要傳遞給Scan調(diào)用的內(nèi)容。并且鑒于您擁有的輸入是 a reflect.Type,您可以推斷出如何從中獲得Type要傳遞到的正確類型的對(duì)象Scan。
- 1 回答
- 0 關(guān)注
- 177 瀏覽
添加回答
舉報(bào)