The internal mechanism of variables
go language variable is divided into two parts:
- Type of information: predefined meta information
- Value: The process can be run dynamically changing.
Reflection introduction
Reflection is the ability to access and modify the program itself in the program run. Program at compile time, the variable is converted to a memory address, variable names are not written to the compiler executable section. When you run the program, the program can not obtain their own information.
Support can be reflected in the language reflected the information the compiler will variables, such as the field name, type information, structure information integrated into the executable file, and the program provides an interface to access information reflected, so that you can get in a program run type of reflection information, and the ability to modify them.
In the go language, empty interface can store any type of variable, what is this empty interface to get hold of the data, it is necessary to use reflection.
reflect package
Any interface in reflection value can be appreciated by reflect.Type
and reflect.Value
composed of two parts, and reflect package provided reflect.TypeOf
and reflect.ValueOf
two arbitrary object to obtain the functions and Value Type.
TypeOf
In the go language, the use of reflect.TypeOf()
the function can get any type of object worth (reflect.Type), the program can access the type of information to any value by type of object.
func reflectType(x interface{}) {
v := reflect.typeOf(x)
fmt.Printf("type:%v\n", v)
}
func main () {
var a float32 = 3.14
reflectType(a) // type: float32
}
type name和type kind
In the reflection type is also divided into two: 类型(Type)
and 种类(Kind)
. Because we can go in using the language keyword type structure many custom type, and 种类(Kind)
refers to the type of bottom, but in reflection, when it is necessary to distinguish between a large variety of types of pointers, structures, etc., will be used 种类(Kind)
.
package main
import (
"fmt"
"reflect"
)
type myInt int64
func reflectType(x interface{}) {
t := reflect.TypeOf(x)
fmt.Printf("type:%v kind:%v\n", t.Name(), t.Kind())
}
func main() {
var a *float32
var b myInt
var c rune
reflectType(a) // type: kind:ptr
reflectType(b) // type:myInt kind:int64
reflectType(c) // type:int32 kind:int32
type person struct {
name string
age int32
}
var d = person{
name: "张三",
age: 18,
}
reflectType(d) // type:person kind:struct
}
In reflect
Kind package type definition is as follows:
const (
Invalid Kind = iota // 非法类型
Bool // 布尔型
Int // 有符号整型
Int8 // 有符号8位整型
Int16 // 有符号16位整型
Int32 // 有符号32位整型
Int64 // 有符号64位整型
Uint // 无符号整型
Uint8 // 无符号8位整型
Uint16 // 无符号16位整型
Uint32 // 无符号32位整型
Uint64 // 无符号64位整型
Uintptr // 指针
Float32 // 单精度浮点数
Float64 // 双精度浮点数
Complex64 // 64位复数类型
Complex128 // 128位复数类型
Array // 数组
Chan // 通道
Func // 函数
Interface // 接口
Map // 映射
Ptr // 指针
Slice // 切片
String // 字符串
Struct // 结构体
UnsafePointer // 底层指针
)
ValueOf
reflect.ValueOf()
It returns reflect.Value
the type, which contains the value information of the original value. reflect.Value
Between the original value and can be interchangeable.
reflect.Value
Obtain the original value of the type of method are as follows: