From e0a9b939b500ce0c826f2e00cb7452b62ca2f0f2 Mon Sep 17 00:00:00 2001 From: huangzj Date: Fri, 24 Apr 2020 15:12:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(reflect=20tool)=EF=BC=9A=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E5=8F=8D=E5=B0=84=E5=B7=A5=E7=A8=8B=EF=BC=8C=E6=8F=90=E4=BE=9B?= =?UTF-8?q?=E5=9B=9B=E4=B8=AA=E5=9F=BA=E7=A1=80=E5=8F=8D=E5=B0=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MainTest.go | 54 +++++++++++++++++++++ readme.md | 7 +++ util/FiledValueTool.go | 105 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 MainTest.go create mode 100644 readme.md create mode 100644 util/FiledValueTool.go diff --git a/MainTest.go b/MainTest.go new file mode 100644 index 0000000..35ea9d5 --- /dev/null +++ b/MainTest.go @@ -0,0 +1,54 @@ +/* + * @Author : huangzj + * @Time : 2020/4/24 14:51 + * @Description: + */ + +package main + +import ( + "GoReflectTool/util" + "fmt" + "reflect" +) + +func main() { + testReflect() +} + +func testReflect() { + entity := Entity{ + Num: 1, + S: "1223", + T: make(map[string]string), + } + structMap := util.GetStructFieldTypeMap(entity) + fmt.Println("根据结构体获取Map结构") + for key, val := range structMap { + fmt.Println(fmt.Sprintf("结构体属性,key: %v ,value: %v ", key, val)) + } + + strMap := util.GetPtrFieldTypeMap(&entity) + fmt.Println("根据结构体的指针对象获取Map结构") + for key, val := range strMap { + fmt.Println(fmt.Sprintf("结构体属性,key: %v ,value: %v ", key, val)) + } + + ptrList := util.GetPtrTypeFieldNameList(reflect.TypeOf(&entity)) + fmt.Println("根据结构体指针类型获取对应的属性名") + for _, val := range ptrList { + fmt.Println(fmt.Sprintf("字段名:%s ", val)) + } + + structList := util.GetStructTypeFieldNameList(reflect.TypeOf(entity)) + fmt.Println("根据结构体类型获取对应所有字段名称") + for _, val := range structList { + fmt.Println(fmt.Sprintf("字段名:%s ", val)) + } +} + +type Entity struct { + Num int + S string + T map[string]string +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..524953f --- /dev/null +++ b/readme.md @@ -0,0 +1,7 @@ +# 作用 +      本工程仅供个人学习记录,刚开始学习go(之前是java),本工程积累一些反射的用法。后期查询更加快捷 + +       + +# 更新日志 +2020/4/24 :创建FiledValueTool.go四个方法,分别处理【结构体对象】、【结构体指针对象】的字段名-字段值的map获取以及 【结构体类型】、【结构体指针类型】的字段名获取 \ No newline at end of file diff --git a/util/FiledValueTool.go b/util/FiledValueTool.go new file mode 100644 index 0000000..4b173a2 --- /dev/null +++ b/util/FiledValueTool.go @@ -0,0 +1,105 @@ +/* + * @Author : huangzj + * @Time : 2020/4/24 12:00 + * @Description: + */ + +package util + +import "reflect" + +/* + * + * 方法仅支持【结构体对象】 + * + * @param 结构体对象 + * @return 字段名和字段值对应的map + * @description + */ +func GetStructFieldTypeMap(i interface{}) map[string]interface{} { + //应该在最前面这边有一个判断才对 + if GetValueType(i) != reflect.Struct { + panic("该方法只支持结构体对象") + } + + var m map[string]interface{} + m = make(map[string]interface{}) + + t := reflect.TypeOf(i) + v := reflect.ValueOf(i) + for i := 0; i < t.NumField(); i++ { + fn := t.Field(i).Name + val := v.Field(i).Interface() + m[fn] = val + } + return m +} + +/* + * + * 方法仅支持【结构体的指针对象】 + * + * @param 结构体的指针对象 + * @return 字段名和字段值对应的map + * @description + */ +func GetPtrFieldTypeMap(i interface{}) map[string]interface{} { + + var m map[string]interface{} + m = make(map[string]interface{}) + + ind := reflect.Indirect(reflect.ValueOf(i)) + t := ind.Type() + for i := 0; i < t.NumField(); i++ { + fn := t.Field(i).Name + val := ind.Field(i).Interface() + m[fn] = val + } + + return m +} + +/* + * + * 根据结构体类型获取结构体字段的名称数组 + * + * @param + * @return + * @description + */ +func GetStructTypeFieldNameList(t reflect.Type) []string { + list := make([]string, 0) + + for i := 0; i < t.NumField(); i++ { + list = append(list, t.Field(i).Name) + + } + return list +} + +/* + * + * 根据结构体类型的指针获取结构体字段的名称数组 + * + * @param + * @return + * @description + */ +func GetPtrTypeFieldNameList(t reflect.Type) []string { + list := make([]string, 0) + + typ := t.Elem() + + for i := 0; i < typ.NumField(); i++ { + list = append(list, typ.Field(i).Name) + + } + return list +} + +/* + * @description 判断传进来的参数是什么类型的 + */ +func GetValueType(value interface{}) reflect.Kind { + return reflect.TypeOf(value).Kind() +}