package utils import ( "reflect" "unicode" "unicode/utf8" ) func StructToMap(item interface{}) map[string]interface{} { res := map[string]interface{}{} if item == nil { return res } v := reflect.TypeOf(item) reflectValue := reflect.ValueOf(item) reflectValue = reflect.Indirect(reflectValue) if v.Kind() == reflect.Ptr { v = v.Elem() } for i := 0; i < v.NumField(); i++ { tag := v.Field(i).Tag.Get("json") if !isExported(v.Field(i).Name) { continue } field := reflectValue.Field(i).Interface() if tag != "" && tag != "-" { if v.Field(i).Type.Kind() == reflect.Struct { res[tag] = StructToMap(field) } else { res[tag] = field } } } return res } func isExported(name string) bool { rune, _ := utf8.DecodeRuneInString(name) return unicode.IsUpper(rune) }