func rowsToMap(rows *sql.Rows) []map[string]string {
cols, _ := rows.Columns()
mappedRows := []map[string]string{}
// Create a slice of interface{}'s to represent each column,
// and a second slice to contain pointers to each item in the columns slice.
columns := make([]interface{}, len(cols))
columnPointers := make([]interface{}, len(cols))
for rows.Next() {
for i := range columns {
columnPointers[i] = &columns[i]
}
// Scan the result into the column pointers...
if err := rows.Scan(columnPointers...); err != nil {
continue
}
// Create our map, and retrieve the value for each column from the pointers slice,
// storing it in the map with the name of the column as the key.
mappedRow := make(map[string]string)
for i, colName := range cols {
val := columnPointers[i].(*interface{})
if *val == nil {
mappedRow[colName] = ""
} else {
str := fmt.Sprintf("%s", *val)
mappedRow[colName] = str
}
}
// Outputs: map[columnName:value columnName2:value2 columnName3:value3 ...]
mappedRows = append(mappedRows, mappedRow)
}
return mappedRows
}