package main
import (
"fmt"
"reflect"
)
type Ifer interface {
DD()
}
type Ins struct{}
func (ins Ins) DD() {}
func main() {
lg := true
typ := reflect.TypeOf(lg)
fmt.Println("lg: ", typ.Kind())
s := struct{}{}
typ = reflect.TypeOf(s)
fmt.Println("s: ", typ.Kind())
var ins If = Ins{}
typ = reflect.TypeOf(ins)
fmt.Println("ins: ", typ.Kind())
}
上述代码输出为:
lg: bool
s: struct
ins: struct
lg
和 s
的 Kind 没有问题. 问题在于把 If
接口类型的 ins
传递给 TypeOf()
, 其 Kind 为 struct.
- 为什么传递接口, 而 Kind 仍为 struct?
- 应该传递什么, Kind 才为 interface?
之所以有这个问题是因为源码中的
type Kind uint
的可取值包括Interface
.
有疑问加站长微信联系(非本文作者)

确实不常见,但也不是没有,比如:
interface 其实有点像C++里的通用指针,可以指向所有类型 数据类型;
而Kind返回的是数据的真实类型;