Files
Huangzj 5d9b7282ab feat(Go-Tool):
2021/1/4 :reflectM包添加indirect处理指针和非指针,获取type、value
2021-01-04 15:49:21 +08:00

59 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* @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,
})))
}