Go   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了golang reflect包基本用法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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包基本用法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。