将界面转换为其相关地图

Convert interface to its respecting map

例如,如果我有一个 interface{} 值,最初是 map[string]map[int64][]int64 或任何其他类型的映射,如何获取映射的键类型?或者更准确地说,如何将其转换为 map[theKeyType]interface{}?

func Transverse(any interface{}) string {
  res := ``
  switch any.(type) {
    case string:
      return ``
    case []byte:
      return ``
    case int, int64, int32:
      return ``
    case float32, float64:
      return ``
    case bool:
      return ``
    case map[int64]interface{}:
      return ``
    case map[string]interface{}:
      return ``
    case []interface{}:
      return ``
    default:
      kind := reflect.TypeOf(any).Kind()
      switch kind {
      case reflect.Map:
        // how to convert it to map[keyType]interface{} ?
      }
      return `` // handle other type
   }
   return ``
}

获取密钥类型很容易:

reflect.TypeOf(any).Key()

要进行整个转换,您需要创建类型为 map[keyType]interface{} 的地图值,然后将这些值复制过来。下面是一个如何做到这一点的工作示例:

package main

import (
    "errors"
    "fmt"
    "reflect"   
)

func InterfaceMap(i interface{}) (interface{}, error) {
    // Get type
    t := reflect.TypeOf(i)

    switch t.Kind() {
    case reflect.Map:
        // Get the value of the provided map
        v := reflect.ValueOf(i)

        // The "only" way of making a reflect.Type with interface{}
        it := reflect.TypeOf((*interface{})(nil)).Elem()

        // Create the map of the specific type. Key type is t.Key(), and element type is it
        m := reflect.MakeMap(reflect.MapOf(t.Key(), it))

        // Copy values to new map
        for _, mk := range v.MapKeys() {            
            m.SetMapIndex(mk, v.MapIndex(mk))
        }

        return m.Interface(), nil

    }

    return nil, errors.New("Unsupported type")
}

func main() {
    foo := make(map[string]int)
    foo["anisus"] = 42

    bar, err := InterfaceMap(foo)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%#v\n", bar.(map[string]interface{}))
}

输出:

map[string]interface {}{"anisus":42}

游乐场: http://play.golang.org/p/tJTapGAs2b