Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get value form nested map type map[string]interface{}

Tags:

go

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?

like image 445
Unknown Avatar asked Sep 06 '25 00:09

Unknown


1 Answers

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

like image 168
bayrinat Avatar answered Sep 10 '25 03:09

bayrinat