地图上的golang类型断言令人恐慌[重复]

This question already has an answer here:

Type assertion on a map doesn't work, is this the right way to do it?

Just to elaborate, my goal is to return a map with dynamic types. this example is just for demonstration.

package main

import "fmt"

func main()  {
    m := hello().(map[string]int)
    fmt.Println(m)
}

func hello() interface{} {
    return map[string]interface{} {
        "foo": 2,
        "bar": 3,
    }
}

it panics

panic: interface conversion: interface {} is map[string]interface {}, not map[string]int

</div>

return the appropriate type

package main

import "fmt"

func main()  {
    m := hello().(map[string]int)
    fmt.Println(m)
}

func hello() interface{} {
    return map[string]int{
        "foo": 2,
        "bar": 3,
    }
}

To answer my own question, assertion works on map values

a few examples

package main

import "fmt"

type person struct {
    name string
}

func main()  {
    m := example1().(map[string]interface{})

    fmt.Println(m["foo"].(int))

    m2 := example2().(map[string]interface{})

    fmt.Println(m2["foo"].(person).name)

}

func example1() interface{} {
    return map[string]interface{} {
        "foo": 2,
        "bar": 3,
    }
}

func example2() interface{} {
    m := make(map[string]interface{})

    m["foo"] = person{"Name is foo!"}
    m["bar"] = person{"Name is bar!"}

    return m
}