add struct to map

This commit is contained in:
Jay 2018-03-12 18:07:05 +08:00
parent 3873486b30
commit 2f4bd7faf2

View File

@ -2,6 +2,7 @@ package libs
import (
"flag"
"reflect"
)
// Flags - flag values struct
@ -27,3 +28,16 @@ func RegFlag(f *Flags) {
flag.BoolVar(&f.Override, "y", false, "if output file exists override")
flag.StringVar(&f.Password, "p", "", "private key password")
}
func (f *Flags) ToMap() map[string]interface{} {
t := reflect.ValueOf(f).Elem()
smap := make(map[string]interface{})
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
smap[t.Type().Field(i).Name] = f.Interface()
}
return smap
}