25 lines
424 B
Go
25 lines
424 B
Go
package pkg
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
func StructToMap(obj interface{}) map[string]interface{} {
|
|
val := reflect.ValueOf(obj)
|
|
if val.Kind() == reflect.Ptr {
|
|
val = val.Elem()
|
|
}
|
|
|
|
result := make(map[string]interface{})
|
|
typ := val.Type()
|
|
|
|
for i := 0; i < val.NumField(); i++ {
|
|
field := val.Field(i)
|
|
fieldName := strings.ToLower(typ.Field(i).Name)
|
|
result[fieldName] = field.Interface()
|
|
}
|
|
|
|
return result
|
|
}
|