无法将值放入映射中。

huangapple go评论92阅读模式
英文:

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

huangapple
  • 本文由 发表于 2017年6月7日 15:28:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/44406213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定