interface{} 是一个类型,reflect.TypeOf() 需要一个值。所以你不能将文字 interface{} 传递给它。你只能传递一个值。
回到原来的问题。让我们看一个struct 的例子:
type My struct {
A int
B interface{}
C io.Reader
}
您想判断字段的类型是否为interface{}。获取struct类型的reflect.Type,则可以使用Type.Field()或Type.FieldByName()访问字段。
这会为您提供一个类型为 reflect.StructField 的值,用于存储字段的类型。
到目前为止一切顺利。但是我们应该将它与什么进行比较? interface{} 是一个有 0 个方法的接口类型。您不能拥有(实例化)该类型的值。您只能拥有具体类型的值,但是可以,它们可以包装在接口类型中。
您可以使用Type.Kind,并将其与reflect.Interface 进行比较,后者会告诉您它是否是一个接口,但这是所有接口类型的true。你也可以用Type.NumMethod()检查它是否有0个方法,interface{}必须为0,但其他接口也可以有0个方法...
你可以使用Type.Name,但由于interface{}是一个未命名类型,它的名字是空字符串""(还有其他未命名的类型)。你可以使用Type.String(),它返回"interface {}"作为空接口:
t := reflect.TypeOf(My{})
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
fmt.Printf("Field %q, type: %-12v, type name: %-8q, is interface{}: %v\n",
f.Name, f.Type,
f.Type.Name(),
f.Type.String() == "interface {}",
)
}
输出(在Go Playground上试试):
Field "A", type: int , type name: "int" , is interface{}: false
Field "B", type: interface {}, type name: "" , is interface{}: true
Field "C", type: io.Reader , type name: "Reader", is interface{}: false
您可能会发现这个相关问题很有趣/有用:Identify non builtin-types using reflect