go-ml.dev/pkg/base@v0.0.0-20200610162856-60c38abac71b/fu/keysof.go (about) 1 package fu 2 3 import ( 4 "reflect" 5 ) 6 7 /* 8 KeysOf returns list of keys of a map 9 */ 10 func KeysOf(m interface{}) interface{} { 11 v := reflect.ValueOf(m) 12 if v.Kind() != reflect.Map { 13 panic("parameter is not a map") 14 } 15 k := v.MapKeys() 16 keys := reflect.MakeSlice(reflect.SliceOf(v.Type().Key()), len(k), len(k)) 17 for i, s := range k { 18 keys.Index(i).Set(s) 19 } 20 return keys.Interface() 21 } 22 23 /* 24 SortedKeysOf returns sorted list of keys of a map 25 */ 26 func SortedKeysOf(m interface{}) interface{} { 27 keys := KeysOf(m) 28 Sort(keys) 29 return keys 30 }