分享
Golang通过反射检测变量类型
90design · · 3157 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
What you are wasting today is tomorrow for those who died yesterday; what you hate now is the future you can not go back.
你所浪费的今天是昨天死去的人奢望的明天; 你所厌恶的现在是未来的你回不去的曾经。
简单的记录一下吧。
package main
import (
"reflect"
"fmt"
"strings"
)
type Foo struct {
A int `tag1:"Tag1" tag2:"Second Tag"`
B string
}
func main(){
f := Foo{A: 10, B: "Salutations"}
fPtr := &f
m := map[string]int{"A": 1 , "B":2}
ch := make(chan int)
sl:= []int{1,32,34}
str := "string var"
strPtr := &str
tMap := examiner(reflect.TypeOf(f), 0)
tMapPtr := examiner(reflect.TypeOf(fPtr), 0)
tMapM := examiner(reflect.TypeOf(m), 0)
tMapCh := examiner(reflect.TypeOf(ch), 0)
tMapSl := examiner(reflect.TypeOf(sl), 0)
tMapStr := examiner(reflect.TypeOf(str), 0)
tMapStrPtr := examiner(reflect.TypeOf(strPtr), 0)
fmt.Println("tMap :", tMap)
fmt.Println("tMapPtr: ",tMapPtr)
fmt.Println("tMapM: ",tMapM)
fmt.Println("tMapCh: ",tMapCh)
fmt.Println("tMapSl: ",tMapSl)
fmt.Println("tMapStr: ",tMapStr)
fmt.Println("tMapStrPtr: ",tMapStrPtr)
}
func examiner(t reflect.Type, depth int) map[int]map[string]string{
outType := make(map[int]map[string]string)
outType = map[int] map[string]string{depth:{"Name":t.Name(), "Kind":t.Kind().String()}}
// 如果需要继续检测元素的类型
switch t.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice:
outType = make(map[int]map[string]string)
tMap := examiner(t.Elem(), depth)
for k, v := range tMap{
outType[k] = v
}
case reflect.Struct:
outType = make(map[int]map[string]string)
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
outType[i] = map[string]string{
"Name":f.Name,
"Kind":f.Type.String(),
}
}
}
return outType
}
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信3157 次点击
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
What you are wasting today is tomorrow for those who died yesterday; what you hate now is the future you can not go back.
你所浪费的今天是昨天死去的人奢望的明天; 你所厌恶的现在是未来的你回不去的曾经。
简单的记录一下吧。
package main
import (
"reflect"
"fmt"
"strings"
)
type Foo struct {
A int `tag1:"Tag1" tag2:"Second Tag"`
B string
}
func main(){
f := Foo{A: 10, B: "Salutations"}
fPtr := &f
m := map[string]int{"A": 1 , "B":2}
ch := make(chan int)
sl:= []int{1,32,34}
str := "string var"
strPtr := &str
tMap := examiner(reflect.TypeOf(f), 0)
tMapPtr := examiner(reflect.TypeOf(fPtr), 0)
tMapM := examiner(reflect.TypeOf(m), 0)
tMapCh := examiner(reflect.TypeOf(ch), 0)
tMapSl := examiner(reflect.TypeOf(sl), 0)
tMapStr := examiner(reflect.TypeOf(str), 0)
tMapStrPtr := examiner(reflect.TypeOf(strPtr), 0)
fmt.Println("tMap :", tMap)
fmt.Println("tMapPtr: ",tMapPtr)
fmt.Println("tMapM: ",tMapM)
fmt.Println("tMapCh: ",tMapCh)
fmt.Println("tMapSl: ",tMapSl)
fmt.Println("tMapStr: ",tMapStr)
fmt.Println("tMapStrPtr: ",tMapStrPtr)
}
func examiner(t reflect.Type, depth int) map[int]map[string]string{
outType := make(map[int]map[string]string)
outType = map[int] map[string]string{depth:{"Name":t.Name(), "Kind":t.Kind().String()}}
// 如果需要继续检测元素的类型
switch t.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice:
outType = make(map[int]map[string]string)
tMap := examiner(t.Elem(), depth)
for k, v := range tMap{
outType[k] = v
}
case reflect.Struct:
outType = make(map[int]map[string]string)
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
outType[i] = map[string]string{
"Name":f.Name,
"Kind":f.Type.String(),
}
}
}
return outType
}