当前位置: 首页 > >

golang struct to map

发布时间:

golang struct to map



func Struct2Map(obj interface{}) map[string]interface{} {
t := reflect.TypeOf(obj)
v := reflect.ValueOf(obj)

var data = make(map[string]interface{})
for i := 0; i < t.NumField(); i++ {
data[strings.ToLower(t.Field(i).Name)] = v.Field(i).Interface()
}
return data
}


或者直接用开源库:
"github.com/fatih/structs"



友情链接: