1-reflect.Type

1 implementation file

gofrontend\libgo\go\reflect\type.go
gofrontend\libgo\go\reflect\value.go

This defines the type of internal
src \ the reflect \ type.go
src \ Runtime \ type.go

2. reflect.Type

reflect.Type is an interface.
defines a set of methods. such as

2.1 Public Methods
2.1.1 Name Related:
  1. type name Name() string
  2. Type of string String() string
2.1.2 Type Related:
  1. Type of type valueKind() Kind
  2. Whether the type can be convertedConvertibleTo(u Type) bool
2.1.3 RELATED:
  1. The method of obtaining the i-th. Method(int) Method
  2. The number of methods. NumMethod() int
  3. Acquisition method by name MethodByName(string) (Method, bool)
Different types of specific methods 2.2
2.2.1 sub-element type Array, Chan, Map, Ptr, or Slice call
  1. Returns the inner sub-element type Elem() Type
2.2.2 struct unique method
  1. Get the i-th field. Field(i int) StructField
  2. Gets a field by name FieldByName(name string) (StructField, bool)
  3. The number of fields. NumField() int
2.2.3 map unique method
  1. Return key type,Key() Type
2.2.4 array-specific methods
  1. Array length Len() int
2.2.5 func unique method
  1. The reference number NumIn() int
  2. Returns the number of values NumOut() int
  3. The i-th Return Type Out(i int) Type
  4. The i-th parameter type In(i int) Type
2.2.6 chan unique method
  1. chan direction ChanDir() ChanDir
type ChanDir int

const (
    RecvDir ChanDir             = 1 << iota // <-chan
    SendDir                                 // chan<-
    BothDir = RecvDir | SendDir             // chan
)
// Create the main IR data structure.
type Type interface {
    Align() int                 // 此类型的变量对齐后所占用的字节数
    FieldAlign() int            // 如果是struct的字段,对齐后占用的字节数
    Method(int) Method          // 返回类型方法集里的第 `i` (传入的参数)个方法
    MethodByName(string) (Method, bool) // 通过名称获取方法
    NumMethod() int             // 获取类型方法集里导出的方法个数
    Name() string               // 类型名称
    PkgPath() string            // 返回类型所在的路径,如:encoding/base64
    Size() uintptr               // 返回类型的大小,和 unsafe.Sizeof 功能类似
    String() string                     // 返回类型的字符串表示形式
    Kind() Kind                         // 返回类型的类型值
    Implements(u Type) bool             // 类型是否实现了接口 u
    AssignableTo(u Type) bool           // 是否可以赋值给 u
    ConvertibleTo(u Type) bool          // 是否可以类型转换成 u
    Comparable() bool                   // 类型是否可以比较
    // 下面这些函数只有特定类型可以调用
    // 如:Key, Elem 两个方法就只能是 Map 类型才能调用
    Bits() int        // 类型所占据的位数
    ChanDir() ChanDir // 返回通道的方向,只能是 chan 类型调用
    // 返回类型是否是可变参数,只能是 func 类型调用
    // 比如 t 是类型 func(x int, y ... float64)
    // 那么 t.IsVariadic() == true
    IsVariadic() bool
    Elem() Type // 返回内部子元素类型,只能由类型 Array, Chan, Map, Ptr, or Slice 调用

    // 返回结构体类型的第 i 个字段,只能是结构体类型调用
    // 如果 i 超过了总字段数,就会 panic
    Field(i int) StructField
    FieldByIndex(index []int) StructField        // 返回嵌套的结构体的字段
    FieldByName(name string) (StructField, bool) // 通过字段名称获取字段
    // FieldByNameFunc returns the struct field with a name
    // 返回名称符合 func 函数的字段
    FieldByNameFunc(match func(string) bool) (StructField, bool)
    In(i int) Type           // 获取函数类型的第 i 个参数的类型
    Key() Type               // 返回 map 的 key 类型,只能由类型 map 调用
    Len() int                // 返回 Array 的长度,只能由类型 Array 调用
    NumField() int           // 返回类型字段的数量,只能由类型 Struct 调用
    NumIn() int              // 返回函数类型的输入参数个数
    NumOut() int             // 返回函数类型的返回值个数
    Out(i int) Type          // 返回函数类型的第 i 个值的类型
    common() *rtype          // 返回类型结构体的相同部分
    uncommon() *uncommonType // 返回类型结构体的不同部分
}

3. Kind

  1. Kind defined using all types of internal, integers, boolean, string, map, slice, chan, struct like.
  2. Kind defined for each string name.
type Kind uint
const (
    Invalid Kind = iota
    Bool
    Int
    Int8
    Int16
    Int32
    Int64
    Uint
    Uint8
    Uint16
    Uint32
    Uint64
    Uintptr
    Float32
    Float64
    Complex64
    Complex128
    Array
    Chan
    Func
    Interface
    Map
    Ptr
    Slice
    String
    Struct
    UnsafePointer
)

src\reflect\type.go
var kindNames = []string{
    Invalid:       "invalid",
    Bool:          "bool",
    Int:           "int",
    Int8:          "int8",
    Int16:         "int16",
    Int32:         "int32",
    Int64:         "int64",
    Uint:          "uint",
    Uint8:         "uint8",
    Uint16:        "uint16",
    Uint32:        "uint32",
    Uint64:        "uint64",
    Uintptr:       "uintptr",
    Float32:       "float32",
    Float64:       "float64",
    Complex64:     "complex64",
    Complex128:    "complex128",
    Array:         "array",
    Chan:          "chan",
    Func:          "func",
    Interface:     "interface",
    Map:           "map",
    Ptr:           "ptr",
    Slice:         "slice",
    String:        "string",
    Struct:        "struct",
    UnsafePointer: "unsafe.Pointer",
}


4. reflect.rtype == runtime._type

reflectIt is defined rtypeas a portion of all common types. And runtimein _typethe same structure.

// rtype must be kept in sync with ../runtime/type.go:/^type._type.

type tflag uint8
type nameOff int32
type typeOff int32
type textOff int32

type _type struct {
    size       uintptr  //8 字节
    ptrdata    uintptr  //8 字节 size of memory prefix holding all pointers
    hash       uint32   //4 字节
    tflag      tflag    //1 字节
    align      uint8    //1 字节  0x08 8字节对象
    fieldalign uint8    //1 字节  0x08 8字节对象 
    kind       uint8    //1 字节   0x14=20=RUNTIME_TYPE_KIND_INTERFACE
    alg        *typeAlg //8 字节
    // gcdata stores the GC type data for the garbage collector.
    // If the KindGCProg bit is set in kind, gcdata is a GC program.
    // Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
    gcdata    *byte     //8
    str       nameOff   //4
    ptrToThis typeOff   //4
}

4.1 chantype


type chantype struct {
    typ  _type
    elem *_type
    dir  uintptr
}

4.2 maptype

type maptype struct {
    typ        _type
    key        *_type
    elem       *_type
    bucket     *_type // internal type representing a hash bucket
    keysize    uint8  // size of key slot
    valuesize  uint8  // size of value slot
    bucketsize uint16 // size of bucket
    flags      uint32
}

4.3 arraytype

type arraytype struct {
    typ   _type
    elem  *_type
    slice *_type
    len   uintptr
}

4.4 slicetype

type slicetype struct {
    typ  _type
    elem *_type
}

4.5 functype

type functype struct {
    typ      _type
    inCount  uint16
    outCount uint16
}

4.6 interfacetype

type interfacetype struct {
    typ     _type
    pkgpath name
    mhdr    []imethod
}

4.7 ptrtype


type ptrtype struct {
    typ  _type
    elem *_type
}

4.8 structtype


type structfield struct {
    name       name
    typ        *_type
    offsetAnon uintptr
}

type structtype struct {
    typ     _type
    pkgPath name
    fields  []structfield
}

// Method on non-interface type
type method struct {
    name nameOff // name of method
    mtyp typeOff // method type (without receiver)
    ifn  textOff // fn used in interface call (one-word receiver)
    tfn  textOff // fn used for normal method call
}


type imethod struct {
    name nameOff
    ityp typeOff
}

Reproduced in: https: //www.jianshu.com/p/4d90b90c4d9d

Guess you like

Origin blog.csdn.net/weixin_34417183/article/details/91244347