Reflection 在go反射包中,调用Value.Kind()是否是Value.Type().Kind()的语法糖?

Reflection 在go反射包中,调用Value.Kind()是否是Value.Type().Kind()的语法糖?,reflection,go,Reflection,Go,接口和类型都实现了相同的Kind()方法签名,假设我们有一些value对象v:=reflect.ValueOf(x) 是v.Kind()只需调用v.Type().Kind()?它们包含相同的值,但似乎不是指相同的东西: 来源 来源 类型通常由未报告的结构rtype(通过TypeOf)实现,而值包含一个*rtype并扩展标志,该标志本身是类型的简化形式: // flag holds metadata about the value. // The lowest bits are flag bi

接口和类型都实现了相同的
Kind()
方法签名,假设我们有一些value对象
v:=reflect.ValueOf(x)


v.Kind()
只需调用
v.Type().Kind()

它们包含相同的值,但似乎不是指相同的东西:

  • 来源
  • 来源
类型
通常由未报告的结构
rtype
(通过
TypeOf
)实现,而
包含一个
*rtype
并扩展
标志
,该标志本身是
类型的简化形式

// 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 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}
}
获取类型时:(
type
是一个接口,通常由
*rtype
实现)


因此,尽管在大多数情况下它们似乎是相等的,
v.Kind()
不是
v.Type().Kind()

文件声明
reflect.Value
实现中的相关字段“除了方法值之外,重复typ.Kind()。因此,除非该值是一个方法,
value.Kind()
value.Type().Kind()
返回相同的数字。

Type是否可以使Kind
无效?这可能是他们不同的一种情况。
func (v Value) Kind() Kind {
    return v.kind()
}

func (f flag) kind() Kind {
    return Kind((f >> flagKindShift) & flagKindMask)
}
func (t *rtype) Kind() Kind { return Kind(t.kind & kindMask) }