获取值形式嵌套地图类型map [string] interface {} [英] get value form nested map type map[string]interface{}

查看:94
本文介绍了获取值形式嵌套地图类型map [string] interface {}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从嵌套地图中获取所有价值,但我不知道该怎么做.

 程序包主要导入"fmt"func main(){m:= map [string] interface {} {创建日期","clientName":"data.user.name",地址":map [string] interface {} {"street":"x.address",},其他":map [string] interface {} {"google":map [string] interface {} {值":map [string] interface {} {"x":"y.address",},},},"new_address":map [string] interface {} {"address":"z.address",},}对于i:=范围m {fmt.Println(m [i])//如何从其他嵌套地图中获取价值?}} 

如何从其他嵌套地图中获取价值?

解决方案

您应该使用非恐慌转换来定位值.

  for i:=范围m {nestedMap,好的:= m [i].(map [string] interface {})如果可以,{//做你想做的}} 

更多详细信息: https://golang.org/ref/spec#Type_assertions

I'm trying to get all value from nested map and, I don't know how I can do that.

package main

import "fmt"

func main() {
    m := map[string]interface{}{
        "date":       "created",
        "clientName": "data.user.name",
        "address": map[string]interface{}{
            "street": "x.address",
        },
        "other": map[string]interface{}{
            "google": map[string]interface{}{
                "value": map[string]interface{}{
                    "x": "y.address",
                },
            },
        },
        "new_address": map[string]interface{}{
            "address": "z.address",
        },
    }

    for i := range m {
        fmt.Println(m[i])
        // how I can get value from other nested map?
    }
}

how I can get value from other nested map?

解决方案

You should use nonpanic casting to target value.

for i := range m {
    nestedMap, ok := m[i].(map[string]interface{})
    if ok {
        // Do what you want
    }
}

More details: https://golang.org/ref/spec#Type_assertions

这篇关于获取值形式嵌套地图类型map [string] interface {}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆