网站图表怎么做的,网站建设及管理基本要求,品牌推广方案思维导图,的网站建设公司那个好2019独角兽企业重金招聘Python工程师标准 反射reflection 可以大大提高程序的灵活性#xff0c;使得interface{}有更大的发挥余地反射可以使用TypeOf和ValueOf函数从接口中获取目标对象信息反射会将匿名字段作为独立字段#xff08;匿名字段的本质#xff09;… 2019独角兽企业重金招聘Python工程师标准 反射reflection 可以大大提高程序的灵活性使得interface{}有更大的发挥余地反射可以使用TypeOf和ValueOf函数从接口中获取目标对象信息反射会将匿名字段作为独立字段匿名字段的本质想要利用反射修改对象状态前提是interface.data是settable即pointer-interface通过反射可以“动态”调用方法 常用的类型、函数和方法 //返回动态类型i的类型如果i是一个空结构体类型TypeOf将返回nil
func TypeOf(i interface{}) Type//Type 接口类型
type Type interface {Align() intFieldAlign() int//指定结构体中方法的下标返回某个方法的对象需要注意的是返回的Method是一个独立的结构体Method(int) Method/*type Method struct {Name stringPkgPath stringType TypeFunc ValueIndex int}*/MethodByName(string) (Method, bool)//返回该结构体类型的方法下标NumMethod() int//返回类型的名称即动态类型i的名称Name() stringPkgPath() stringSize() uintptrString() stringKind() KindImplements(u Type) boolAssignableTo(u Type) boolConvertibleTo(u Type) boolComparable() boolBits() intChanDir() ChanDirIsVariadic() boolElem() Type//返回结构体类型第i个字段Field(i int) StructField//StructField结构体//type StructField struct {// Name string// PkgPath string// Type Type// Tag StructTag// Offset uintptr// Index []int// Anonymous bool//根据结构体字段索引获取嵌入字段的结构体信息FieldByIndex(index []int) StructFieldFieldByName(name string) (StructField, bool)FieldByNameFunc(match func(string) bool) (StructField, bool)In(i int) TypeKey() TypeLen() int//返回动态类型i(结构体字段)的字段总数NumField() intNumIn() intNumOut() intOut(i int) Type
}//返回接口i的一个初始化的新值.ValueOf(nil)返回一个零值
func ValueOf(i interface{}) Value// Value结构体
type Value struct {}
// Value结构体的一些方法
// 返回结构体v中的第i个字段。如果v的类型不是结构体或者i超出了结构体的范围则会出现panic
func (v Value) Field(i int) Value//以接口类型返回v的当前值
func (v Value) Interface() (i interface{})
//等价于.
var i interface{} (vs underlying value)//通过反射方式修改结构体对象的一些方法//返回接口v包含或者指针v包含的值
func (v Value) Elem() Value
//判断该接口v是否可以被set修改
func (v Value) CanSet() bool//使用另外一个反射接口去修改反射值
func (v Value) Set(x Value)
//其他不同类型的Set
func (v Value) SetBool(x bool)
func (v Value) SetBytes(x []byte)
func (v Value) SetFloat(x float64)
func (v Value) SetInt(x int64)
//设置结构体对象v的长度为n
func (v Value) SetLen(n int)
func (v Value) SetString(x string)//一些辅助方法
//返回反射结构体的Value的类型.如果v为零值IsValid将返回false
func (v Value) Kind() Kind
//判断value是否为有效值通常用在判断某个字段是否在反射体的Value中
func (v Value) IsValid() bool//Kind常量
type Kind uint
const (Invalid Kind iotaBoolIntInt8Int16Int32Int64UintUint8Uint16Uint32Uint64UintptrFloat32Float64Complex64Complex128ArrayChanFuncInterfaceMapPtrSliceStringStructUnsafePointer
) 反射的基本操作 通过反射来获取结构体字段的名称以及其他相关信息。 package mainimport (fmtreflect
)//定义结构体
type User struct {Id intName stringAge int
}//定义结构体方法
func (u User) Hello() {fmt.Println(Hello xuxuebiao)
}func main() {u : User{1, bgops, 25}Info(u)u.Hello()
}//定义一个反射函数,参数为任意类型
func Info(o interface{}) {//使用反射类型获取o的Type,一个包含多个方法的interfacet : reflect.TypeOf(o)//打印类型o的名称fmt.Println(type:, t.Name())//使用反射类型获取o的Value,一个空的结构体v : reflect.ValueOf(o)fmt.Println(Fields:)//t.NumField()打印结构体o的字段个数(Id,Name,Age共三个)for i : 0; i t.NumField(); i {//根据结构体的下标i来获取结构体某个字段,并返回一个新的结构体/**type StructField struct {Name stringPkgPath stringType TypeTag StructTagOf 转载于:https://my.oschina.net/u/3683692/blog/3039768