反射:可以在运行时动态获取变量的相关信息, “reflect” 这个包,状态转换如:变量<===>Interface{} <===>Reflect.Value
看一下reflect这个包,type value部分,将一个变量,转换成 reflect.Value之后,提供了很多方法分析这个变量,可以做下面这么多事情。也就是说可以动态地处理很多事情。获取运行时的详细信息。
type Value
func Append(s Value, x ...Value) Value
func AppendSlice(s, t Value) Value
func Indirect(v Value) Value
func MakeChan(typ Type, buffer int) Value
func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value
func MakeMap(typ Type) Value
func MakeMapWithSize(typ Type, n int) Value
func MakeSlice(typ Type, len, cap int) Value
func New(typ Type) Value
func NewAt(typ Type, p unsafe.Pointer) Value
func Select(cases []SelectCase) (chosen int, recv Value, recvOK bool)
func ValueOf(i interface{}) Value
func Zero(typ Type) Value
func (v Value) Addr() Value
func (v Value) Bool() bool
func (v Value) Bytes() []byte
func (v Value) Call(in []Value) []Value
func (v Value) CallSlice(in []Value) []Value
func (v Value) CanAddr() bool
func (v Value) CanInterface() bool
func (v Value) CanSet() bool
func (v Value) Cap() int
func (v Value) Close()
func (v Value) Complex() complex128
func (v Value) Convert(t Type) Value
func (v Value) Elem() Value
func (v Value) Field(i int) Value
func (v Value) FieldByIndex(index []int) Value
func (v Value) FieldByName(name string) Value
func (v Value) FieldByNameFunc(match func(string) bool) Value
func (v Value) Float() float64
func (v Value) Index(i int) Value
func (v Value) Int() int64
func (v Value) Interface() (i interface{})
func (v Value) InterfaceData() [2]uintptr
func (v Value) IsNil() bool
func (v Value) IsValid() bool
func (v Value) Kind() Kind
func (v Value) Len() int
func (v Value) MapIndex(key Value) Value
func (v Value) MapKeys() []Value
func (v Value) MapRange() *MapIter
func (v Value) Method(i int) Value
func (v Value) MethodByName(name string) Value
func (v Value) NumField() int
func (v Value) NumMethod() int
func (v Value) OverflowComplex(x complex128) bool
func (v Value) OverflowFloat(x float64) bool
func (v Value) OverflowInt(x int64) bool
func (v Value) OverflowUint(x uint64) bool
func (v Value) Pointer() uintptr
func (v Value) Recv() (x Value, ok bool)
func (v Value) Send(x Value)
func (v Value) Set(x Value)
func (v Value) SetBool(x bool)
func (v Value) SetBytes(x []byte)
func (v Value) SetCap(n int)
func (v Value) SetComplex(x complex128)
func (v Value) SetFloat(x float64)
func (v Value) SetInt(x int64)
func (v Value) SetLen(n int)
func (v Value) SetMapIndex(key, val Value)
func (v Value) SetPointer(x unsafe.Pointer)
func (v Value) SetString(x string)
func (v Value) SetUint(x uint64)
func (v Value) Slice(i, j int) Value
func (v Value) Slice3(i, j, k int) Value
func (v Value) String() string
func (v Value) TryRecv() (x Value, ok bool)
func (v Value) TrySend(x Value) bool
func (v Value) Type() Type
func (v Value) Uint() uint64
func (v Value) UnsafeAddr() uintptr
下面看一下代码示例: 变量<===>Interface{} <===>Reflect.Value
package main
import (
"fmt"
"reflect"
)
type Student struct {
Name string
Age int
score float32
}
func test(b interface{}) {
//在这里面看到的是空接口,b,可以通过反射看到具体的类型
t := reflect.TypeOf(b)
fmt.Println(t)
//ValueOf func(i interface{}) Value 返回值是Value,可以获取很多信息
v := reflect.ValueOf(b)
k := v.Kind()
fmt.Println(k)
//还可以再将上面的type Value重新转换成interface{},还可以继续转回来
iv := v.Interface() //转换成通用类型
stu, ok := iv.(Student) //转换成具体类型,还顺便判断了一波(这里可以用if/switch)
if ok {
fmt.Printf("%v,%T \n", stu, stu)
}
}
//
func TestInt(b interface{}) {
val := reflect.ValueOf(b) //返回Value这个类型,接下来很多方法可以分析
c := val.Int()
fmt.Printf("get value interface{} %d \n", c)
}
func main() {
var aa int = 100
test(aa)
fmt.Println()
var bb Student = Student{
Name: "stu1",
Age: 18,
score: 92,
}
test(bb)
TestInt(1234)
}
输出结果:
PS F:\go\src\go_dev> .\main.exe
int
int
main.Student
struct
{stu1 18 92},main.Student
get value interface{} 1234
PS F:\go\src\go_dev>