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]]
<a href="/user/dong-hao" title="@dong-hao">@dong-hao</a> 感谢,确实是指向的同一个地址,按照你的方法成功解决,非常感谢
#5
更多评论
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