package main
import "reflect"
type people struct {
name string
age int
}
func test(a interface{}) {
// fmt.Println(a.(people)) 但是如果事先并不知道能类型断言成什么类型的话,如何转换?
rtype := reflect.TypeOf(a)
a.(rtype) // err: rtype is not a type
}
func main() {
var p1 people
test(p1)
}
你都不知道什么类型了怎么转换?rtype是一个结构对象,类型为一个结构体,它实现了 reflect.Type 接口。
不知道你要实现什么会需要这种转换?