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?
makefor the slice of values creates new variables, which will have different memory addresses each time this code is run. The loop is required because you can't get the memory addresses (pointers) of each item in the slice any other way.makecould be executed once near define statement. Or it is way to clean values?makecalls local to the function. If you made them global (which I assume is what you are suggesting), that memory never gets released. If they only exist in the function, all that memory is released when the function exits. It just makes the most sense to declare locally over globally.