aqi/utils/struct2map.go

31 lines
468 B
Go
Raw Normal View History

2024-06-18 18:08:39 +08:00
package utils
import (
"reflect"
)
func StructToMap(input any) map[string]any {
out := make(map[string]any)
v := reflect.ValueOf(input)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
// we only accept structs
if v.Kind() != reflect.Struct {
return nil
}
typ := v.Type()
for i := 0; i < v.NumField(); i++ {
fi := typ.Field(i)
// skip unexported fields
if fi.PkgPath != "" {
continue
}
out[fi.Name] = v.Field(i).Interface()
}
return out
}