go 反射reflect速记

基本概念

在这里插入图片描述

demo

源码:https://gitee.com/luyue_zhang/reflect_fast

reflect 实现简版json.Marshal、json.UnMarshal

使用场景

db中字段列存储json数据

演示

test文件位置:https://gitee.com/luyue_zhang/reflect_fast/blob/master/test/marshal_test.go

在这里插入图片描述

反射实现方法调用

使用场景

rpc服务调用

演示

test文件位置:https://gitee.com/luyue_zhang/reflect_fast/blob/master/test/reflect_method_test.go

在这里插入图片描述

使用反射设置值

使用场景

orm框架selector.get(val any)

演示

test文件位置:https://gitee.com/luyue_zhang/reflect_fast/blob/master/test/set_value_test.go

在这里插入图片描述

.Elem()含义

.Elem()用于获取指针、切片、数组或映射map的基础类型

  • 对于指针*test,.Elem()返回test
  • 对于切片[]*test, .Elem()返回*test // 元素类型
  • 对于数组[5]test, .Elem()返回test // 元素类型
  • 对于映射map[string]test, .Elem()返回test

相关资料

反射

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值