
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())} 总结 以上是内存溢出为你收集整理的golang reflect包基本用法全部内容,希望文章能够帮你解决golang reflect包基本用法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)