Go——反射的使用

目录

1,reflect

2,valueOf

3,value.set

4,结构体的反射

5,函数的反射

6,方法反射


概念:运行时动态的获取变量的相关信息。

反射:对一些对象进行序列化处理

Import ("reflect)

1,reflect

  1. reflect. TypeOf,获取变量的类型,返回reflect Type类型

  2. reflect ValueOf,获取变量的值,返回reflect .Value类型

  3. reflect.Value Kind, 获取变量的类别,返回一个常量

  4. reflect Value .Interface(),转换成interface{}类型

示例:获取变量类型

package main
​
import (
    "fmt"
    "reflect"
)
​
//定义一个函数
func Test(i interface{}) {
    //反射数据类型
    t := reflect.TypeOf(i)
    fmt.Println("类型是:", t)
    //反射数据值
    y := reflect.ValueOf(i)
    fmt.Println("类型是:", y)
}
​
func main() {
    a := "hello"
    Test(a)
}
[Running] go run "f:\goProject\src\dev_code\day23\example1\main.go"
类型是: string
类型是: hello
​
[Done] exited with code=0 in 0.73 seconds

示例:类型和类别

package main
​
import (
    "fmt"
    "reflect"
)
​
//类型和类别
//定义结构体
type student struct {
    name  string
    age   int
    score float32
}
​
//函数传入
func Test(i interface{}) {
    //反射数据类型
    t := reflect.TypeOf(i)
    fmt.Println("类型是:", t)
    //类别
    y := reflect.ValueOf(i) //或者u:=reflect.ValueOf(i).Kind()
    u := y.Kind()
    fmt.Println("值是:", y)
    fmt.Println("类别是:", u)     //类别是一个常量
​
}
​
func main() {
    var stu student
    stu.name = "zhangsan"
    stu.age = 20
    stu.score = 50
    Test(stu)
    fmt.Println("<<<--------我是华丽的分割线--------->>>")
    var num int = 10
    Test(num)
}
[Running] go run "f:\goProject\src\dev_code\day23\example2\main\main.go"
类型是: main.student
值是: {zhangsan 20 50}
类别是: struct
<<<--------我是华丽的分割线--------->>>
类型是: int   
值是: 10
类别是: int
​
[Done] exited with code=0 in 0.6 seconds

示例:断言处理类型转换

package main
​
import (
    "fmt"
    "reflect"
)
​
//类型和类别
//定义结构体
type student struct {
    name  string
    age   int
    score float32
}
​
//函数传入
func Test(i interface{}) {
    //反射数据类型
    t := reflect.TypeOf(i)
    fmt.Println("类型是:", t)
    //类别
    y := reflect.ValueOf(i) //或者u:=reflect.ValueOf(i).Kind()
    u := y.Kind()
    fmt.Println("类别是:", u) //类别是一个常量
    fmt.Printf("y的类型是%T\n", y)
    fmt.Printf("u的类型是%T\n", u)
    //转化成接口
    iy := y.Interface()
    fmt.Printf("iy的类型是%T\n", iy)
    //断言处理iy是student类型是true就赋值给stu_iy
    stu_iy, ok := iy.(student)
    if ok {
        fmt.Printf("stu_iy的类型是%T\n", stu_iy)
    }
}
​
func main() {
    var stu student
    stu.name = "zhangsan"
    stu.age = 20
    stu.score = 50
    Test(stu)
​
}
[Running] go run "f:\goProject\src\dev_code\day23\example2\main\main.go"
类型是: main.student
类别是: struct
y的类型是reflect.Value
u的类型是reflect.Kind
iy的类型是main.student
stu_iy的类型是main.student
​
[Done] exited with code=0 in 0.474 seconds

2,valueOf

获取变量值

reflect. Value0f(x)- Float()
reflect. Value0f(x).Int( )
reflect . ValueOf(x) . String( )
reflect . ValueOf(x). Bool()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值