Reflection 将接口转换为其对应的映射
例如,如果我有一个Reflection 将接口转换为其对应的映射,reflection,go,Reflection,Go,例如,如果我有一个接口{}值,该值最初是映射[string]map[int64][]int64或任何其他类型的映射,如何获取映射的键类型?或者更准确地说,如何将其转换为map[theKeyType]接口{} func Transverse(any interface{}) string { res := `` switch any.(type) { case string: return `` case []byte: return ``
接口{}
值,该值最初是映射[string]map[int64][]int64
或任何其他类型的映射,如何获取映射的键类型?或者更准确地说,如何将其转换为map[theKeyType]接口{}
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}
操场: