I would like to make this function more generic (with a "for")
func (e example) ScanRow(rows *sql.Rows) (example, error) {
val := reflect.ValueOf(e)
test := make([]interface{}, val.Type().NumField())
test[0] = &e.Id
test[1] = &e.CreatedAt
test[2] = &e.Text
test[3] = &e.AuthorId
test[4] = &e.CategoryId
if err := rows.Scan(test[0], test[1], test[2], test[3], test[4]); err != nil {
return e, err
}
return e, nil
}
Here is the struct example:
type example struct {
Id int `json:"id"`
CreatedAt string `json:"created_at"`
Text string `json:"text"`
AuthorId int `json:"author_id"`
CategoryId int `json:"category_id"`
}
Do you think it's possible ? I try to find a way to do that but i am lost...
Aucun commentaire:
Enregistrer un commentaire