go 反射的快速入门

此处的变量reflect.TypeOf, reflect.ValueOf, 返回的 其实表示的不是真正的int,它是 reflect.TypeOf(b) 返回的type Type interface, 接口,里面包含了很多方法 不能 var a int = 10, var a1 rType=20

package main

import (
	"fmt"
	"reflect"
)

//专门演示反射
func reflectTest01(b interface{}) {
	fmt.Printf("06>> iv = %v, type=%T\n",b,b)  // 无法用%T查看出接口类型
	num3 := b.(int)
	fmt.Printf("07>> num2=%v type=%T\n",num3, num3)

	//通过反射获取的传入的变量的type , kind,值
	//1。先获取到 reflect.Type
	// 此处的变量rType 其实表示的不是真正的int,它是 reflect.TypeOf(b) 返回的type Type interface, 接口,里面包含了很多方法 不能 var a int = 10, var a1 rType=20
	rTyp := reflect.TypeOf(b)
	fmt.Printf("01>> rType=%v type=%T \n", rTyp,rTyp)
	//2.获取到 reflect.value
	rVal := reflect.ValueOf(b)
	//n1 := 2 + rVal  此处不能编译,说明rVal也不是真正的int'类型100
	fmt.Printf("02>> rVal=%v  rVal type=%T\n", rVal,rVal)
	// 求出真正的int
	n2 := 2 + rVal.Int()  //
	fmt.Printf("03>> n2=%v  n2 type=%T\n", n2,n2)

	//n3 := rVal.Float()  // 如果取的类型不对, 会报错 panic: reflect: call of reflect.Value.Float on int Value
	//fmt.Println("n3=", n3)	

	//下面我们将 rVal转成interface{}
	iV := rVal.Interface()
	fmt.Printf("04>> iv = %v, type=%T\n",iV,iV)
	//将interface{}通过断言转成需要的类型
	num2 := iV.(int)
	fmt.Printf("05>> num2=%v type=%T\n",num2, num2)
}

//专门演示反射
func reflectTest02(b interface{}) {
	//通过反射获取的传入的变量的type , kind,值
	//1。先获取到 reflect.Type
	rTyp := reflect.TypeOf(b)
	fmt.Printf("08>> rType=%v type=%T \n", rTyp,rTyp)

	//2.获取到 reflect.value
	rVal := reflect.ValueOf(b)

	//3.获取变量对应的Kind
	//(1)rval.Kind()==>
	kind1 := rVal.Kind()
	//(2) rTyp.Kind()==>
	kind2 := rTyp.Kind()
	fmt.Printf("09>> kind1 =%v kind2=%v \n", kind1, kind2)


	//下面我们将 rVal转成interface{}
	iV := rVal.Interface()
	fmt.Printf("10>> iv = %v, type=%T\n",iV,iV)
	//将interface{}通过断言转成需要的类型
	//这里,我们就简单使用了一带检测的类型断言。
	//同学们可以使用switch 的断言形式来做的更加的灵活
	stu,ok := iV.(Student)
	if ok {
		fmt.Printf("11>> stu.Name=%v \n", stu.Name)
	}
}

type Student struct {
	Name string
	Age int
}


func main() {
	//请编写一个案例,
	//演示对(基本数据类型、interface{(}、reflect. value)进行反射的基本操作
	//1。先定义一个int
	var num int = 100
	reflectTest01(num)

		//2.定义一个student的实例
	stu := Student{
		Name : "tom",
		Age : 20,
	}
	reflectTest02(stu)
}

输出:

06>> iv = 100, type=int
07>> num2=100 type=int
01>> rType=int type=*reflect.rtype
02>> rVal=100  rVal type=reflect.Value
03>> n2=102  n2 type=int64
04>> iv = 100, type=int
05>> num2=100 type=int
08>> rType=main.Student type=*reflect.rtype
09>> kind1 =struct kind2=struct
10>> iv = {tom 20}, type=main.Student
11>> stu.Name=tom

通过反射指针修改值 Elem的使用

package main

import (
	"fmt"
	"reflect"
)


func reflect01(b interface{}) {
	//2。获取到 reflect.value
	rVal := reflect.ValueOf(b)
	//看看rval的Kind是
	fmt.Printf("rva1 kind=%v \n", rVal.Kind())
	//3.rVal
	// Elem返回v持有的接口保管的值的value封装,或者v持有的指针指向的值的value封装

	rVal.Elem().SetInt(20)
}

func main() {
	var num int = 10
	reflect01(&num)
	fmt.Println("num=", num)  // 20

/*
	你可以这样理解rval.Elem()
	num := 9
	ptr *int = &num
	num2 := *ptr //===类似 rva1.Elem()
*/
}

输出:

rva1 kind=ptr
num= 20

2测试

package main

import (
	"fmt"
	"reflect"
)

func main() {
	type S struct {
		F string `species:"gopher" color:"blue"`
	}
	s := S{}
	fmt.Printf("001 类型=%T,字符=%s,详细类型=%#v \n",s,s,s)
	st := reflect.TypeOf(s)
	fmt.Printf("002 类型=%T,字符=%s,详细类型=%#v \n",st,st,st)
	field := st.Field(0)
	fmt.Println(field.Tag.Get("color"), field.Tag.Get("species"))
}

输出:

001 类型=main.S,字符={},详细类型=main.S{F:""}
002 类型=*reflect.rtype,字符=main.S,详细类型=&reflect.rtype{size:0x10, ptrdata:0x8, hash:0xcdb468b2, tflag:0x7, align:0x8, fieldAlign:0x8, kind:0x19, equal:(func(unsafe.Pointer, unsafe.Pointer) bool)(0x403500), gcdata:(*uint8)(0
x4eaa60), str:4470, ptrToThis:28448}
blue goph
posted @ 2022-03-26 21:21  ty1539  阅读(29)  评论(0编辑  收藏  举报