feat(Go-Tool):
2021/1/4 :reflectM包添加indirect处理指针和非指针,获取type、value
This commit is contained in:
@@ -77,6 +77,8 @@
|
|||||||
|
|
||||||
2020/12/31:删除源码学习部分,挪到Go-Study工程
|
2020/12/31:删除源码学习部分,挪到Go-Study工程
|
||||||
|
|
||||||
|
2021/1/4 :reflectM包添加indirect处理指针和非指针,获取type、value
|
||||||
|
|
||||||
# mod vendor模式加载包
|
# mod vendor模式加载包
|
||||||
通过go mod的方式加载的github上面的包会有报红的问题,但是包本身是可以运行的,这样就是会有一个问题,如果你想要点击去看方法的内容,没办法做到
|
通过go mod的方式加载的github上面的包会有报红的问题,但是包本身是可以运行的,这样就是会有一个问题,如果你想要点击去看方法的内容,没办法做到
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2021/1/4 15:44
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package reflectM
|
||||||
|
|
||||||
|
import "reflect"
|
||||||
|
|
||||||
|
//获取value值,包含ptr的处理
|
||||||
|
func Indirect(reflectValue reflect.Value) reflect.Value {
|
||||||
|
for reflectValue.Kind() == reflect.Ptr {
|
||||||
|
reflectValue = reflectValue.Elem()
|
||||||
|
}
|
||||||
|
return reflectValue
|
||||||
|
}
|
||||||
|
|
||||||
|
//获取Type值,包含ptr的处理
|
||||||
|
func IndirectType(reflectType reflect.Type) reflect.Type {
|
||||||
|
for reflectType.Kind() == reflect.Ptr || reflectType.Kind() == reflect.Slice {
|
||||||
|
reflectType = reflectType.Elem()
|
||||||
|
}
|
||||||
|
return reflectType
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2021/1/4 15:46
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package reflectM
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
testA int
|
||||||
|
testB string
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIndirect(t *testing.T) {
|
||||||
|
i := 20
|
||||||
|
s := "测试"
|
||||||
|
fmt.Println("测试type")
|
||||||
|
test := Test{
|
||||||
|
testA: 10000,
|
||||||
|
testB: "我是测试属性",
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(IndirectType(reflect.TypeOf(i)))
|
||||||
|
fmt.Println(IndirectType(reflect.TypeOf(s)))
|
||||||
|
fmt.Println(IndirectType(reflect.TypeOf(test)))
|
||||||
|
fmt.Println(IndirectType(reflect.TypeOf(&test)))
|
||||||
|
fmt.Println(IndirectType(reflect.TypeOf([]Test{test})))
|
||||||
|
fmt.Println(IndirectType(reflect.TypeOf([]*Test{&test, &test, &test})))
|
||||||
|
fmt.Println(IndirectType(reflect.TypeOf(map[string]Test{
|
||||||
|
"a": test,
|
||||||
|
})))
|
||||||
|
fmt.Println(IndirectType(reflect.TypeOf(map[string]*Test{
|
||||||
|
"a": &test,
|
||||||
|
})))
|
||||||
|
|
||||||
|
fmt.Println("测试value")
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
fmt.Println(Indirect(reflect.ValueOf(i)))
|
||||||
|
fmt.Println(Indirect(reflect.ValueOf(s)))
|
||||||
|
fmt.Println(Indirect(reflect.ValueOf(test)))
|
||||||
|
fmt.Println(Indirect(reflect.ValueOf(&test)))
|
||||||
|
fmt.Println(Indirect(reflect.ValueOf([]Test{test})))
|
||||||
|
fmt.Println(Indirect(reflect.ValueOf([]*Test{&test})))
|
||||||
|
fmt.Println(Indirect(reflect.ValueOf(map[string]Test{
|
||||||
|
"a": test,
|
||||||
|
})))
|
||||||
|
fmt.Println(Indirect(reflect.ValueOf(map[string]*Test{
|
||||||
|
"a": &test,
|
||||||
|
})))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user