How could we implement a function which would return all rows resulting from a SQL query and convert them to dest
which is an interface array (may not work as Scan
) ?
I assume the destination array would have to be given as an argument to the function. But then, I still do not know how I am supposed to complete the implementation:
func GetAll(query string, dest interface{}) error {
rows, err := s.db.Query(query)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var destRow ??? /* do not have a type. using reflect.TypeOf(dest).Elem()? */
err := rows.Scan(&destRow)
if err != nil {
return err
}
dest = append(dest, destRow) /* would even compile? */
}
return nil
}
It does not look like much different from what json.Unmarshal
have to do actually...