答案 0 :(得分:3)
使用reflect包对任意地图类型进行操作:
func GetMapKey(reference interface{}, key string) (interface{}, error) {
m := reflect.ValueOf(reference)
if m.Kind() != reflect.Map {
return nil, errors.New("not a map")
}
v := m.MapIndex(reflect.ValueOf(key))
if !v.IsValid() {
return nil, errors.New("The " + key + " key was not present in the map")
}
return v.Interface(), nil
}