Go beginner here, I am trying to create a generic routine to extract the values from a map, I have this right now:
func getValues(m map[interface{}]interface{}) []interface{} {
v := make([]interface{}, 0, len(m))
for _, value := range m {
v = append(v, value)
}
return v
}
and I called it like so:
nearby := make(map[string]Nearby)
values := getValues(nearby)
but I get this error:
cannot use nearby (type map[string]Nearby) as type map[interface {}]interface {} in argument to getValues
It's usually best to just write the type specific code. To answer your question though, use the reflect package:
func getValues(m interface{}) []interface{} {
v := reflect.ValueOf(m)
result := make([]interface{}, 0, v.Len())
for _, k := range v.MapKeys() {
result = append(result, v.MapIndex(k).Interface())
}
return result
}