心有法竹
它们包含相同的值,但似乎不是指同一件事:type.go源码value.go源码AType通常由未导出的结构rtype(via TypeOf)实现,而Value包含 a*rtype和 extends flag,它本身是 的简化形式Kind:// flag holds metadata about the value.// The lowest bits are flag bits:// - flagRO: obtained via unexported field, so read-only// - flagIndir: val holds a pointer to the data// - flagAddr: v.CanAddr is true (implies flagIndir)// - flagMethod: v is a method value.// The next five bits give the Kind of the value.// This repeats typ.Kind() except for method values.// The remaining 23+ bits give a method number for method values.// If flag.kind() != Func, code can assume that flagMethod is unset.// If typ.size > ptrSize, code can assume that flagIndir is set.拿到ValueOf东西时:// ValueOf returns a new Value initialized to the concrete value// stored in the interface i. ValueOf(nil) returns the zero Value.func ValueOf(i interface{}) Value { [...] // For an interface value with the noAddr bit set, // the representation is identical to an empty interface. eface := *(*emptyInterface)(unsafe.Pointer(&i)) typ := eface.typ /** Flag is built from the type, then kept separate (my comment) */ fl := flag(typ.Kind()) << flagKindShift if typ.size > ptrSize { fl |= flagIndir } return Value{typ, unsafe.Pointer(eface.word), fl}}所以当你得到一个 Value 类型时(记住它扩展了它的标志):func (v Value) Kind() Kind { return v.kind()}func (f flag) kind() Kind { return Kind((f >> flagKindShift) & flagKindMask)}同时获取一种类型:(Type是一个接口,通常由 实现*rtype)func (t *rtype) Kind() Kind { return Kind(t.kind & kindMask) }因此,尽管他们似乎是在大多数的情况下平等,v.Kind()是不 v.Type().Kind()