英文:
Can't put value into map
问题
我刚学习了Golang,并且想要构建一个像这样的结构体。
{end:false map["h":{false "h" map["e":{true "e" map[]}]}
"s":{false "s" map["h":{false "h" map["e":{true "e" map[]}]}]}
]}
以下是我编写的代码:
package main
import "fmt"
type node struct {
end bool
char string
children map[int32]node
}
func addKeyword(root *node, key string) {
current := root
for _, v := range key {
mm := current.children
if nil == mm || len(mm) == 0 {
mm = make(map[int32]node)
current.children = mm
}
child, ok := mm[v]
if !ok {
child = node{false, string(v), nil}
mm[v] = child
}
current = &child
}
current.end = true
}
func main() {
root := new(node)
addKeyword(root, "he")
addKeyword(root, "she")
fmt.Println(root)
}
我得到的结果是:
{end:false map["h":{false "h" map[]}
"s":{false "s" map[]}
]}
我不知道为什么第二层结构没有添加到根对象中。
英文:
I just learn golang, and want to build an struct like this.
{end:false map["h":{false "h" map["e":{true "e" map[]}]}
"s":{false "s" map["h":{false "h" map["e":{true "e" map[]}]}]}
]}
Follow is the code I write:
package main
import "fmt"
type node struct {
end bool
char string
children map[int32]node
}
func addKeyword(root *node, key string) {
current := root
for _, v := range key {
mm := current.children
if nil == mm || len(mm) == 0 {
mm = make(map[int32]node)
current.children = mm
}
child, ok := mm[v]
if !ok {
child = node{false, string(v), nil}
mm[v] = child
}
current = &child
}
current.end = true
}
func main() {
root := new(node)
addKeyword(root, "he")
addKeyword(root, "she")
fmt.Println(root)
}
I get result is:
{end:false map["h":{false "h" map[]}
"s":{false "s" map[]}
]}
I don't know why second level struct not append to root object.
答案1
得分: 1
问题与children
映射的类型有关。您在该映射中使用了node
值。结果是,每次访问键时,您都会获得新的值,并且更改不会反映到原始的“parent”节点中。
问题出在这一行上:
child, ok := mm[v]
child
是一个新变量,它等于node
mm[v]
的值。您所做的更改只会影响到这个值,而mm
中的node
值保持不变。
要解决这个问题,您可以在children
的值中使用*node
:
type node struct {
end bool
char string
children map[int32]*node
}
并相应地修复代码:
// ...
mm := current.children
if nil == mm {
mm = make(map[int32]*node)
current.children = mm
}
child, ok := mm[v]
if !ok {
child = &node{false, string(v), nil}
mm[v] = child
}
current = child
// ...
工作示例:https://play.golang.org/p/XcmPY4Nx-O
英文:
The problem is related to the type of children
map. You are using node
values in this map. As a result, you get new values every time you access a key and the changes are not reflected into the original 'parent' node.
The fault lies with this line:
child, ok := mm[v]
child
is a new variable that is equal to the value of node
mm[v]
. The changes you make happen to this value while the node
value in mm
remains unchanged.
To fix this problem, you can use *node
for values in children
:
type node struct {
end bool
char string
children map[int32]*node
}
And fix the code accordingly:
// ...
mm := current.children
if nil == mm {
mm = make(map[int32]*node)
current.children = mm
}
child, ok := mm[v]
if !ok {
child = &node{false, string(v), nil}
mm[v] = child
}
current = child
// ...
Working example: https://play.golang.org/p/XcmPY4Nx-O
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论