Contents

go标准库-Reflect

go语言中文网

godoc

go语言中文网有很多文档缺少内容比如string.Builder就没有,godoc绝对详尽,推荐阅读godoc

博客园 itbsl

常用函数

  • func DeepEqual(a1, a2 interface{}) bool:用来判断两个值是否深度一致:除了类型相同;在可以时(主要是基本类型)会使用==;但还会比较array、slice的成员,map的键值对,结构体字段进行深入比对。map的键值对,对键只使用==,但值会继续往深层比对。DeepEqual函数可以正确处理循环的类型。函数类型只有都会nil时才相等;空切片不等于nil切片;还会考虑array、slice的长度、map键值对数。

type Kind uint

Kind代表Type类型值表示的具体分类。nil表示Invalid Kind。相当于物理结构分类。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
)

方法

  • func (k Kind) String() string:返回对应的字符串

type StructField struct

常用变量

  • Name string
  • PkgPath string
  • Type Type // 字段的类型
  • Tag StructTag // 字段的标签
  • Anonymous bool // 是否匿名字段

type StructTag string

结构体字段的标签,string类型

方法

  • func (tag StructTag) Get(key string) string:Get方法返回标签字符串中键key对应的值

type Method struct

Method代表一个方法

变量

  • Name string
  • PkgPath string
  • Type Type // 方法类型
  • Func Value // 方法的值

type Type interface

Type类型用来表示一个go类型,它是一个接口

函数

  • func TypeOf(i interface{}) Type:返回接口i对应的Type类型,nil会返回nil

方法

  • Kind() Kind:返回Kind类型
  • Name() string:返回包内类型名
  • PkgPath() string:返回包路径
  • Size() uintptr:返回类型字节数
  • FieldByName(name string) (StructField, bool):返回第一个name字段,非结构体panic
  • NumIn() int:返回有几个入参,非func panic
  • In(i int) Type:返回第i个入参,非func panic
  • MethodByName(string) (Method, bool):返回方法
  • Elem() Type:返回指向的类型,非Array、Chan、Map、Ptr或Slice panic
  • Align() int:返回内存对齐字节数
  • Len() int:返回array字位数
  • Key() Type:返回map的键类型

type Value struct

函数

  • func ValueOf(i interface{}) Value:返回i转化为的Value
  • func Zero(typ Type) Value:返回typ的零值Value对象

方法

  • func (v Value) Kind() Kind:返回Kind对象
  • func (v Value) Type() Type:返回type对象
  • func (v Value) Call(in []Value) []Value:调用该方法,非func panic
  • func (v Value) CanSet() bool:是否可修改
  • func (v Value) SetXxx:修改Xxx为Kind中可修改的值
 |