Retrieving values from nested map in golang

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

Retrieving values from nested map in golang

问题

我正在尝试从一个嵌套的 map 中获取一个值。我已经按照在线教程的步骤进行了操作,但是没有得到正确的答案。这是我的程序:

  1. type OptionMap map[string]interface{}
  2. func(options OptionMap) {
  3. opt, _ := options["data2"].(OptionMap)
  4. fmt.Println("opt", opt)
  5. for key, value := range options {
  6. fmt.Println("Key:", key, "Value:", value)
  7. }
  8. }

options 有两个键 data1 和 data2。在 for 循环中,printf 打印出以下内容:

  1. Key: data1 Value: false
  2. Key: data2 Value: map[h:5]

当我运行以下代码时:

  1. opt, _ := options["data2"].(OptionMap)

我得到的 optnil。我不确定如何获取 map[h:5] 的值。

英文:

I'm trying to retrieving a value from a map within a map. I have followed online tutorials but not getting the right answer. This is my program:

  1. type OptionMap map[string]interface{}
  2. func(options OptionMap) {
  3. opt, _ := options["data2"].(OptionMap)
  4. fmt.Println("opt", opt)
  5. for key, value := range options {
  6. fmt.Println("Key:", key, "Value:", value)
  7. }
  8. }

options has two keys data1 and data2 . Inside for loop the printf prints following

  1. Key: data1 Value: false
  2. Key: data2 Value: map[h:5]

When I run the code

  1. opt, _ := options["data2"].(OptionMap)

I'm getting nil in opt. I'm not sure how to retrieve value of map[h:5].

答案1

得分: 1

你在获取内部映射的值时得到了nil值,这是因为你没有使用OptionMap类型创建内部映射。你可能是使用了map[string]interface{}来创建它,并试图断言为OptionMap类型,但在获取nil值时失败了。请参考下面的示例,该示例也使用了OptionMap类型。请查看类型断言页面:https://tour.golang.org/methods/15

  1. package main
  2. import "fmt"
  3. type OptionMap map[string]interface{}
  4. func main() {
  5. opt := make(OptionMap)
  6. ineeropt := make(OptionMap) // 在创建映射时指定OptionMap类型
  7. ineeropt["h"] = 5
  8. opt["data1"] = false
  9. opt["data2"] = ineeropt
  10. test(opt)
  11. }
  12. func test(options OptionMap) {
  13. opt, _ := options["data2"].(OptionMap) // 如果内部映射是使用OptionMap创建的,则opt不会为nil
  14. fmt.Println("opt", opt)
  15. fmt.Println("opt", options)
  16. for key, value := range options {
  17. fmt.Println("Key:", key, "Value:", value)
  18. }
  19. }

希望对你有帮助!

英文:

You are getting nil value for inner map because you have not created inner map using type OptionMap.You must have created it using map[string]interface{} and trying to assert to OptionMap which is failing in getting nil value. See below example which is working with OptionMap type,too.Go through type assertion page at https://tour.golang.org/methods/15

  1. package main
  2. import "fmt"
  3. type OptionMap map[string]interface{}
  4. func main() {
  5. opt := make(OptionMap)
  6. ineeropt := make(OptionMap) // while creating Map specify OptionMap type
  7. ineeropt["h"]=5
  8. opt["data1"] = false
  9. opt["data2"] = ineeropt
  10. test(opt)
  11. }
  12. func test(options OptionMap) {
  13. opt, _ := options["data2"].(OptionMap) //If inner map is created using OptionMap then opt won't be nil
  14. fmt.Println("opt", opt)
  15. fmt.Println("opt", options)
  16. for key, value := range options {
  17. fmt.Println("Key:", key, "Value:", value)
  18. }
  19. }

huangapple
  • 本文由 发表于 2017年4月12日 08:07:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/43358131.html
匿名

发表评论

匿名网友

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

确定