import ( "reflect" "testing" ) type Users struct { ID int Name string } type TestInterface interface { GetName() string } func (u *Users) UpdateName(newName string) { u.Name = newName } func (u *Users) GetName() string { return u.Name } func TestReflect(t *testing.T) { u := Users{1,"mike"} //返回指定对象的Kind类型 t.Log(reflect.TypeOf(32).Kind()) t.Log(reflect.ValueOf(32).Kind()) //根据方法名找方法 t.Log(reflect.TypeOf(&u).MethodByName("UpdateName")) t.Log(reflect.ValueOf(&u).MethodByName("UpdateName")) //返回第i个方法 t.Log(reflect.TypeOf(&u).Method(0)) t.Log(reflect.ValueOf(&u).Method(0)) //返回拥有的方法总数,包括unexported方法 t.Log(reflect.TypeOf(&u).NumMethod()) t.Log(reflect.ValueOf(&u).NumMethod()) //取struct结构的第n个field t.Log(reflect.TypeOf(u).Field(0)) t.Log(reflect.ValueOf(u).Field(1)) //嵌套的方式取struct的field,比如v.FieldByIndex(1,2,3)等价于 v.field(1).field(2).field(3) t.Log(reflect.TypeOf(u).FieldByIndex([]int{0})) t.Log(reflect.ValueOf(u).FieldByIndex([]int{0})) //返回名称匹配match函数的field t.Log(reflect.TypeOf(u).FieldByName("ID")) t.Log(reflect.ValueOf(u).FieldByName("Name")) //返回struct所包含的field数量 t.Log(reflect.TypeOf(u).NumField()) t.Log(reflect.ValueOf(u).NumField()) //分配内存时的内存对齐字节数 t.Log(reflect.TypeOf(u).Align()) //作为struct的field时内存对齐字节数 t.Log(reflect.TypeOf(u).FieldAlign()) //type名 string类型 t.Log(reflect.TypeOf(u).Name()) //包路径, "encoding/base64", 内置类型返回empty string t.Log(reflect.TypeOf(u).PkgPath()) //该类型变量占用字节数 t.Log(reflect.TypeOf(u).Size()) //type的string表示方式 t.Log(reflect.TypeOf(u).String()) //判断该类型是否实现了某个接口 t.Log(reflect.TypeOf(u).Implements(reflect.TypeOf((*TestInterface)(nil)).Elem())) //判断该类型能否赋值给某个类型 t.Log(reflect.TypeOf(u).AssignableTo(reflect.TypeOf(Users{}))) //判断该类型能否转换为另外一种类型 t.Log(reflect.TypeOf(u).ConvertibleTo(reflect.TypeOf(1))) //判断该类型变量是否可以比较 t.Log(reflect.TypeOf(u).Comparable()) //取该类型的元素,指针指向的结构 t.Log(reflect.TypeOf(&u).Elem()) //调用函数 t.Log(reflect.ValueOf(&u).MethodByName("GetName").Call([]reflect.Value{})) //判断能否取地址 t.Log(reflect.ValueOf(&u).CanAddr()) //判断Interface方法能否使用 t.Log(reflect.ValueOf(&u).CanInterface()) //判断值能否改变 t.Log(reflect.ValueOf(&u).CanSet()) a := []int{0,1} //获取容量 Array/Chan/Slice t.Log(reflect.ValueOf(a).Cap()) c := make(chan int) //关闭channel reflect.ValueOf(c).Close() //返回指针实际的值 t.Log(reflect.ValueOf(&u).Elem()) //索引操作 Array/Slice/String t.Log(reflect.ValueOf(a).Index(0)) //修改数组第一个索引的值 reflect.ValueOf(a).Index(0).Set(reflect.ValueOf(1)) t.Log(a[0]) //将当前value以interface形式返回 t.Log(reflect.ValueOf(&u).Interface()) //判断是否为nil,chan,func,interface,map,pointer,or slice valu t.Log(reflect.ValueOf(&u).IsNil()) //是否是可操作的Value,返回false表示为zero Value t.Log(reflect.ValueOf(&u).IsValid()) //获取长度,适用于Array,Chan,Map,Slice,or String t.Log(reflect.ValueOf(a).Len()) m := map[int]string{1: "Mike",2: "Tom"} //对map类型按key取值 t.Log(reflect.ValueOf(m).MapIndex(reflect.ValueOf(1))) //map类型的所有key的列表 for index,key := range reflect.ValueOf(m).MapKeys() { t.Log("key=",key) t.Log("idnex=",index) } //返回value的Type t.Log(reflect.ValueOf(1).Type()) }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。