I found an answer about how to call the Scan variadic function in Golang using reflection. And don't have reputation to ask in there.
Here the main part of code:
values := make([]interface{}, count)
valuePtrs := make([]interface{}, count)
for rows.Next() {
for i, _ := range columns {
valuePtrs[i] = &values[i]
}
rows.Scan(valuePtrs...)
...
}
And I don't get why is this statement have to be looped? Why for
is in for rows.Next
?
for rows.Next() {
for i, _ := range columns { valuePtrs[i] = &values[i] }
..
}
Would valuePtrs be different on each iteration in golang? Or it's just mistake?
As I understand it from : https://golang.org/pkg/database/sql/#Rows.Scan
"Scan copies the columns in the current row into the values pointed at by dest. "
When you are passing it in specific fields you would use:
rows.scan(&myfield1, &myfield2)
But in your instance you are passing in an array, and the address of the array is not really what you want to send in, Rayfen is right in his comment above - consider the alternative:
rows.scan(&values)
That would be a pointer to a single object, not an array of pointers which is what you really are after, so you have to perform that loop so you can send in an array of pointers to the destination variables, not a pointer to the array.