var data = make([]map[string]interface{}, 0)
var area = make(map[string]interface{})
maps_temp := []map[string]interface{}{{"id":1, "value":"a"},{"id":2, "value":"b"},{"id":3, "value":"c"}}
for _, value := range maps_temp {
area["id"] = value["id"]
area["value"] = value["value"]
data = append(data, area)
}
fmt.Println("data:",data)
The result is
data: [map[id:3 value:c] map[id:3 value:c] map[id:3 value:c]]
expected result is :
data: [map[id:1 value:a] map[id:2 value:b] map[id:3 value:c]]
如果一定要按照楼主的思路写的话,可以这么修改:
```
for _, value := range maps_temp {
area["id"] = value["id"]
area["value"] = value["value"]
data = append(data, area)
area = make(map[string]interface{})
}
fmt.Println("data:", data)
```
#4
更多评论
for loop 中,value 的变量被重复使用了,就是说,你三次
```
area["id"]
area["value"]
```
指向的都是同一片地址。
你在这里,可以这么修改:
```
for i, _ := range maps_temp {
value := maps_temp[i]
area["id"] = value["id"]
area["value"] = value["value"]
data = append(data, area)
}
```
#2