func DoQuery(db *sql.DB, sqlInfo string, args ...interface{}) ([]map[string]interface{}, error) {
rows, err := db.Query(sqlInfo, args...)
if err != nil {
return nil, err
}
columns, _ := rows.Columns()
columnLength := len(columns)
cache := make([]interface{}, columnLength) //临时存储每行数据
for index, _ := range cache { //为每一列初始化一个指针
var a interface{}
cache[index] = &a
}
var list []map[string]interface{} //返回的切片
for rows.Next() {
_ = rows.Scan(cache...)
item := make(map[string]interface{})
for i, data := range cache {
item[columns[i]] = *data.(*interface{}) //取实际类型
}
list = append(list, item)
}
_ = rows.Close()
return list, nil
}