反射是什么
Go 语言提供了一种机制在运行时更新变量和检查它们的值、调用它们的方法,但是在编译时并不知道这些变量的具体类型,这称为反射机制。
反射的作用
- 在编写不定传参类型函数的时候,或传入类型过多时
- 不确定调用哪个函数,需要根据某些条件来动态执行
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)
//--------------------------------------------------
func bridge(funcPtr interface{}, args ...interface{})
1.反射对象包含了接口变量中存储的值以及类型。
2.如果反射对象中包含的值是原始值,那么可以通过反射对象修改原始值;
3.如果反射对象中包含的值不是原始值(反射对象包含的是副本值或指向原始值的地址),则该反射对象不可以修改。