Retrieving values from nested map in golang

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

Retrieving values from nested map in golang

问题

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

type OptionMap map[string]interface{}

func(options OptionMap) {
    opt, _ := options["data2"].(OptionMap)
    fmt.Println("opt", opt)
    for key, value := range options {
        fmt.Println("Key:", key, "Value:", value)
    }
}

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

Key: data1 Value: false
Key: data2 Value: map[h:5]

当我运行以下代码时:

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:

type OptionMap map[string]interface{}
func(options OptionMap) {
    opt, _ := options["data2"].(OptionMap)
    fmt.Println("opt", opt)
    for key, value := range options {
	    fmt.Println("Key:", key, "Value:", value)
    }
}

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

Key: data1 Value: false
Key: data2 Value: map[h:5]

When I run the code

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

package main

import "fmt"

type OptionMap map[string]interface{}

func main() {
    opt := make(OptionMap)
    ineeropt := make(OptionMap) // 在创建映射时指定OptionMap类型
    ineeropt["h"] = 5
    opt["data1"] = false
    opt["data2"] = ineeropt
    test(opt)
}

func test(options OptionMap) {
    opt, _ := options["data2"].(OptionMap) // 如果内部映射是使用OptionMap创建的,则opt不会为nil
    fmt.Println("opt", opt)
    fmt.Println("opt", options)
    for key, value := range options {
        fmt.Println("Key:", key, "Value:", value)
    }
}

希望对你有帮助!

英文:

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

package main

import "fmt"



type OptionMap map[string]interface{}

func main()  {
	opt := make(OptionMap)
	ineeropt := make(OptionMap)     // while creating Map specify OptionMap type
	ineeropt["h"]=5
	opt["data1"] = false
	opt["data2"] = ineeropt
	test(opt)


}
func test(options OptionMap) {
	opt, _ := options["data2"].(OptionMap)  //If inner map is created using OptionMap then opt won't be nil
	fmt.Println("opt", opt)
	fmt.Println("opt", options)
	for key, value := range options {
		fmt.Println("Key:", key, "Value:", value)
	}
}

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:

确定