简要介绍使用Golang反射的两种应用场景及代码示例
反射常见应用场景有以下两种: 1、不知道接口调用哪个函数,根据传入参数在运行时确定调用的具体接口,这种需要对函数或方法反射。例如以下这种桥接模式:
func bridge(funcPtr interface{}, args ...interface{})第一个参数funcPtr以接口的形式传入函数指针,函数参数args以可变参数的形式传入,bridge函数中可以用反射来动态执行funcPtr函数。 2、不知道传入函数的参数类型,函数需要在运行时处理任意参数对象,这种需要对结构体对象反射。典型应用场景是ORM,gorom示例如下:
type User struct {
gorm.Model
Name string
Age sql.NullInt64
Birthday *time.Time
Email string `gorm:"type:varchar(100);unique_index"`
Role string `gorm:"size:255"` // set field size to 255
MemberNumber *string `gorm:"unique;not null"` // set member number to unique and not null
Num int `gorm:"AUTO_INCREMENT"` // set num to auto incrementable
Address string `gorm:"index:addr"` // create index with name `addr` for address
IgnoreMe int `gorm:"-"` // ignore this field
}
var users []User
db.Find(&users)示例中Find函数不知道传入的参数是什么类型,但要能处理任意任意参数。如果类型合法返回正确的值,否则返回异常。
Go反射常用的两种数据类型reflect.Type和reflect.Value,这两种都属于结构体类型,reflect.Type用于反射变量类型信息, reflect.Value用于反射运行时的数据。反射对性能有一定的影响,使用时要考虑对性能的要求。为便于演示创建一个Go单元测试文件 reflect_test.go,Go语言的对象是以结构体的形式使用,自定义一个用于测试的结构体user。
如果喜欢这里的文章,而且又不差钱的话,欢迎打赏个早餐 ^_^
This Site was built by Rimond, generated with Jekyll, and hosted on GitHub Pages
©2013-2018 – Rimond