英文:
golang updating slice value is not reflected in container object
问题
package main
import (
"fmt"
)
func main() {
root := map[string]interface{}{
"disney": "world",
}
fmt.Printf("main begin %v\n", root)
addList(root)
fmt.Printf("main after addList %v\n", root)
addMap(root)
fmt.Printf("main after addMap %v\n", root)
}
func addList(root map[string]interface{}) {
root["list"] = make([]interface{}, 0, 3)
mylist := root["list"].([]interface{})
mylist = append(mylist, "mickeymouse")
fmt.Printf("addList %v\n", mylist)
fmt.Printf("addList %v\n", root)
}
func addMap(root map[string]interface{}) {
root["map"] = make(map[string]interface{})
mymap := root["map"].(map[string]interface{})
mymap["donald"] = "duck"
fmt.Printf("addMap %v\n", mymap)
fmt.Printf("addMap %v\n", root)
}
我有一个根映射,其中包含键值对 "disney"->"world"。在 addList 函数中,我向根映射添加了一个包含 "mickeymouse" 的切片,然后在 addMap 函数中添加了一个键值对 "donald"->"duck" 的映射。然而,切片没有被添加,而映射被添加到了根映射中。子映射是预期的行为,但切片的添加似乎是异常的。我以为切片和映射一样是引用类型的。这是怎么回事?在 Java 中这样做是可以的。我错在哪里?我该如何使其工作?在实际的更大问题中,我不能除了从函数返回错误之外返回任何东西。
英文:
package main
import (
"fmt"
)
func main() {
root := map[string]interface{} {
"disney": "world",
}
fmt.Printf("main begin %v\n", root)
addList(root)
fmt.Printf("main after addList %v\n", root)
addMap(root)
fmt.Printf("main after addMap %v\n", root)
}
func addList(root map[string]interface{}) {
root["list"] = make([]interface{},0,3)
mylist := root["list"]
mylist = append(mylist.([]interface{}),"mickeymouse")
fmt.Printf("addList %v\n", mylist)
fmt.Printf("addList %v\n", root)
}
func addMap(root map[string]interface{}) {
root["map"] = make(map[string]interface{})
mymap := root["map"]
mymap.(map[string]interface{})["donald"] = "duck"
fmt.Printf("addMap %v\n", mymap)
fmt.Printf("addMap %v\n", root)
}
I have a root map having the pair "disney"->"world" . To that root map I added a slice having "mickeymouse" in function addList , followed by adding a map with pair "donald"->"duck" in function addMap. However the slice does not get added, whereas the map gets added to the root map. The child map is expected behavior, but the slice addition seems to be an anomaly. I thought the slice was a reference just like a map in golang. What is going on in this case ? It would have worked in Java. Where am I going wrong ? How can I make it work ? In the actual larger problem I dont have the option of returning anything except an error from the functions.
答案1
得分: 1
append
函数返回一个新的切片。- 在
map
中存储值不会产生一个新的map。
因此,你看不到新的切片,但你可以看到map的内容更新了。
> 我该如何使它工作?
将新的切片存储在旧的位置上,例如:
mylist := root["list"]
mylist = append(mylist.([]interface{}), "mickeymouse")
root["list"] = mylist
// 或者,一行完成
root["list"] = append(root["list"].([]any), "mickeymouse")
英文:
- The
append
function returns a new slice. - Storing values in a
map
will not produce a new map.
It is therefore only natural that you do not see the new slice, but you do see the map's content updated.
> How can I make it work ?
Store the new slice in place of the old, e.g.
mylist := root["list"]
mylist = append(mylist.([]interface{}),"mickeymouse")
root["list"] = mylist
// or, doing it on one line
root["list"] = append(root["list"].([]any), "mickeymouse")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论