🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## int操作和结构体操作 ``` package main import ( "fmt" "reflect" ) //对int反射 func reflectTest01(b interface{}) { //1.获取reflect.Type rTyp := reflect.TypeOf(b)     fmt.Printf("rType=%v\n", rTyp) //rType=int //2.获取reflect.value rVal := reflect.ValueOf(b)     fmt.Printf("rValue=%v rValue的类型是%T\n", rVal, rVal) //rValue=100 rValue的类型是reflect.Value n2 := rVal.Int() + 2 //将reflect.value转回int     fmt.Println("n2=", n2) //n2 = 102 //将rval转成interface{} iv := rVal.Interface() num2 := iv.(int)     fmt.Println("num2=", num2) } //对结构体反射 func reflectTest02(b interface{}) { //1.获取reflect.Type rTyp := reflect.TypeOf(b)     fmt.Printf("rType=%v\n", rTyp) //rType=int //2.获取reflect.value rVal := reflect.ValueOf(b)     fmt.Printf("rValue=%v rValue的类型是%T\n", rVal, rVal) //rValue=100 rValue的类型是reflect.Value //3.获取变量对应的kind //(1)rVal.Kind() kind1 := rVal.Kind() //(2) rTyp.Kind() kind2 := rTyp.Kind()     fmt.Printf("kind=%v kind=%v\\n", kind1, kind2) //kind=struct kind=struct //将rval转成interface{} iv := rVal.Interface()     fmt.Printf("iv=%v iv type=%T\n", iv, iv) //此时无法取出name age value, ok := iv.(Student) if !ok { return //转换失败     } fmt.Printf("name%v age=%v\n", value.Name, value.Age) } type Student struct { Name string Age  int } func main() { num := 100 reflectTest01(num) stu := Student{"Tom", 20} reflectTest02(stu) } ```