如何重置嵌套地图中的值

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

How to reset value in map of maps

问题

这是我的代码:

{
    saruman := map[string]int{
        "power": 150,
        "skills": 200,
        "knowledge": 380,
    }

    gandalf := map[string]int{
        "power": 180,
        "skills": 210,
        "knowledge": 240,
    }

    wizards := make(map[string]interface{})
    wizards["saruman"] = saruman
    wizards["gandalf"] = gandalf

    fmt.Println(wizards)
    // 输出:map[gandalf:map[knowledge:240 power:180 skills:210] saruman:map[knowledge:380 power:150 skills:200]]

    // 修改 gandalf 的属性
    wizards["gandalf"]["knowledge"] = 310

    fmt.Println(wizards)
}

在最后一行,我们得到了以下错误信息:

NOK 无效操作:wizards["gandalf"]["knowledge"](类型 interface {} 不支持索引操作)

英文:

Here's my code

{
    saruman := map[string]int{
     "power": 150,
     "skills": 200,
     "knowledge": 380,
    }

    gandalf := map[string]int{
     "power": 180,
     "skills": 210,
     "knowledge": 240,
    }

    wizards := make(map[string]interface{})
    wizards["saruman"] = saruman
    wizards["gandalf"] = gandalf

    fmt.Println(wizards)
    //OK map[gandalf:map[knowledge:240 power:180 skills:210] saruman:map[knowledge:380 power:150 skills:200]]

    //change gandalf property
    wizards["gandalf"]["knowledge"] = 310

    fmt.Println(wizards)
}

In the last line, we get:

> NOK invalid operation: wizards["gandalf"]["knowledge"] (type interface {} does not support indexing)

答案1

得分: 1

你不能使用["key"]来访问golang不知道的结构。因为它是一个interface{},gandalf后面的值可以是任何类型。

你必须指定你正在交互的数据类型。

Playground: https://goplay.tools/snippet/O62ara8JbUv

英文:

You cannot use ["key"] to access a structure that golang does not know about. Because it is an interface{} the value behind gandalf could be anything.

	wizards["gandalf"] = map[string]int{
	     "knowledge": 310,
    }

You have to specify the type of data you are interacting with.

Playground: https://goplay.tools/snippet/O62ara8JbUv

答案2

得分: 0

需要进行类型转换。

{
    saruman := map[string]int{
        "power":     150,
        "skills":    200,
        "knowledge": 380,
    }

    gandalf := map[string]int{
        "power":     180,
        "skills":    210,
        "knowledge": 240,
    }

    wizards := make(map[string]interface{})
    wizards["saruman"] = saruman
    wizards["gandalf"] = gandalf

    fmt.Println(wizards)
    // 输出:map[gandalf:map[knowledge:240 power:180 skills:210] saruman:map[knowledge:380 power:150 skills:200]]

    // 修改 gandalf 的属性
    wizards["gandalf"].(map[string]int)["knowledge"] = 10
    fmt.Println(wizards)
}

以上是代码的翻译结果。

英文:

Need to cast it.

{
	saruman := map[string]int{
		"power":     150,
		"skills":    200,
		"knowledge": 380,
	}

	gandalf := map[string]int{
		"power":     180,
		"skills":    210,
		"knowledge": 240,
	}

	wizards := make(map[string]interface{})
	wizards["saruman"] = saruman
	wizards["gandalf"] = gandalf

	fmt.Println(wizards)
	//OK map[gandalf:map[knowledge:240 power:180 skills:210] saruman:map[knowledge:380 power:150 skills:200]]

	//change gandalf property
	wizards["gandalf"].(map[string]int)["knowledge"] = 10
	fmt.Println(wizards)
}

答案3

得分: 0

这是更简单的方法:
一行代码

wizards["gandalf"].(map[string]int)["knowledge"] = 110

完整示例:

saruman := map[string]int{
    "power":     150,
    "skills":    200,
    "knowledge": 380,
}
gandalf := map[string]int{
    "power":     180,
    "skills":    210,
    "knowledge": 240,
}

wizards := make(map[string]interface{})
wizards["saruman"] = saruman
wizards["gandalf"] = gandalf

// fmt.Println(wizards)
// OK map[gandalf:map[knowledge:240 power:180 skills:210] saruman:map[knowledge:380 power:150 skills:200]]

// 修改gandalf的属性

wizards["gandalf"].(map[string]int)["knowledge"] = 110
fmt.Println(wizards["gandalf"].(map[string]int)["knowledge"])
英文:

This is the easier way:<br>
one line code

wizards[&quot;gandalf&quot;].(map[string]int)[&quot;knowledge&quot;] = 110

Full example:

saruman := map[string]int{
    &quot;power&quot;:     150,
	&quot;skills&quot;:    200,
	&quot;knowledge&quot;: 380,
}
gandalf := map[string]int{
	&quot;power&quot;:     180,
	&quot;skills&quot;:    210,
	&quot;knowledge&quot;: 240,
}

wizards := make(map[string]interface{})
wizards[&quot;saruman&quot;] = saruman
wizards[&quot;gandalf&quot;] = gandalf

// fmt.Println(wizards)
// OK map[gandalf:map[knowledge:240 power:180 skills:210] saruman:map[knowledge:380 power:150 skills:200]]

// change gandalf property

wizards[&quot;gandalf&quot;].(map[string]int)[&quot;knowledge&quot;] = 110
fmt.Println(wizards[&quot;gandalf&quot;].(map[string]int)[&quot;knowledge&quot;])

huangapple
  • 本文由 发表于 2021年12月10日 17:36:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/70302436.html
匿名

发表评论

匿名网友

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

确定