This is a implementation of dim, which is built in python. But this time in go.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

35 lines
808 B

package query
import (
"database/sql"
"fmt"
)
// RowsToMap converts a query result to a list of maps.
func RowsToMap(rows *sql.Rows) ([]map[string]interface{}, error) {
empty := []map[string]interface{}{}
res := []map[string]interface{}{}
cols, err := rows.Columns()
if err != nil {
return empty, fmt.Errorf("could not get columns: %v", err)
}
for rows.Next() {
if rows.Err() != nil {
return empty, fmt.Errorf("could not iterate rows: %v", err)
}
raw := make([]interface{}, len(cols))
for i, _ := range raw {
raw[i] = new(interface{})
}
if err := rows.Scan(raw...); err != nil {
return empty, fmt.Errorf("could not scan row: %v", err)
}
row := map[string]interface{}{}
for i, col := range cols {
row[col] = raw[i]
}
res = append(res, row)
}
return res, nil
}