I am populating lists of structs from a postgres database using the native sql library and jmoiron/sqlx. I currently have separate functions for different types, for example:
func selectAccounts(ext sqlx.Ext, query string, args []interface{}) ([]Account, error) {
var accts []Account
rows, err := ext.Queryx(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var a Account
if err = rows.StructScan(&a); err != nil {
return nil, err
}
accts = append(accts, a)
}
err = rows.Err()
return accts, err
}
... but I have a requirement to build a generic function that accepts interface{} and returns a []interface{}. I am finding the process of replacing the line var a Account
above is difficult, as it requires making a copies of the passed in interface{} type while replicating its underlying type. Should I resign myself to the fact that I need to be using reflection, or is there another way to go about doing this?