type Person struct{
name string
age int}funccheckType(i interface{}){switch v := i.(type){//这里是通过i.(type)来判断是什么类型 下面的case分支匹配到了 则执行相关的分支caseint:
fmt.Printf("%v is an int\n", v)casestring:
fmt.Printf("%v is string", v)case Person:
fmt.Println("Person", reflect.TypeOf(i))
i2 := i.(Person)//将 i 强转为 Person
fmt.Println(i2.name)casebool:
fmt.Printf("%v is bool", v)}}funcmain(){var info Person
info.name ="zs"
info.age =20checkType(info)}
go interface{}类型转换目录查看interface{}类型还原interface{}的原类型1. 查看interface{}类型func checkType(i interface{}) { reflect.TypeOf(i)}2. 还原interface{}的原类型通过switch判断interface{}类型,然后通过 xxx.(类型) 强转type Person struct { name string age int}func checkTy