I'm looking to call the Rows.Scan() function using reflection. However it takes a variable number of pointers, but there are not a lot of source examples. I need to use reflection because I plan on filling a slice with the values from a Query call. So basically using rows.Columns()
to get the length of the row and then make()
a slice of []interface{}
to fill with the data points that would normally be filled using the pointers passed to the Scan()
function.
Basically something like this code:
col := rows.Columns()
vals := make([]interface{}, len(cols))
rows.Scan(&vals)
Anyone have an example of calling a variadic function that takes pointers using reflection that I can take a look at?
Edit: Sample code that doesn't appear to do what I'm after.
package main
import (
_ "github.com/lib/pq"
"database/sql"
"fmt"
)
func main() {
db, _ := sql.Open(
"postgres",
"user=postgres dbname=Go_Testing password=ssap sslmode=disable")
rows, _ := db.Query("SELECT * FROM _users;")
cols, _ := rows.Columns()
for rows.Next() {
data := make([]interface{}, len(cols))
rows.Scan(data...)
fmt.Println(data)
}
}
The results:
[<nil> <nil> <nil> <nil> <nil>]
[<nil> <nil> <nil> <nil> <nil>]
[<nil> <nil> <nil> <nil> <nil>]
[<nil> <nil> <nil> <nil> <nil>]
[<nil> <nil> <nil> <nil> <nil>]
[<nil> <nil> <nil> <nil> <nil>]