Golang将SQL多行查询结果写入Map

C 2月前 156

var list []map[string]interface{}
for rows.Next() {
	// 构造缓冲并接收
	buffer := make([]interface{}, len(columns))
	for key := range buffer {
		buffer[key] = &buffer[key]
	}
	rows.Scan(buffer...)
	// 将缓冲映射到行Map
	row := make(map[string]interface{})
	for key, val := range columns {
		row[val] = buffer[key]
	}
	// 加入帖子列表
	list = append(list, row)
}
最新回复 (0)