I am trying to make a recursive routine that prints the elements of a complex json
func printMap(m map[string]interface{}) {
for k, v := range m {
typ := reflect.ValueOf(v).Kind()
if typ == reflect.Map {
printMap(v)
} else {
fmt.Println(k, v)
}
} }
but I get a build error can use type v ( type interface {} ) as type map[string] interface{}
Is there a way to type cast it or someway I can get it to work?
Use a type assertion:
func printMap(m map[string]interface{}) {
for k, v := range m {
m, ok := v.(map[string]interface{}) // <-- assert that v is a map
if ok {
printMap(m)
} else {
fmt.Println(k, v)
}
}
}