将接口转换为相应的映射。

huangapple go评论76阅读模式
英文:

Convert interface to its respecting map

问题

例如,如果我有一个interface{}值,它最初是一个map[string]map[int64][]int64或任何其他类型的映射,如何获取映射的键类型?或者更准确地说,如何将其转换为map[键类型]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:
          // 如何将其转换为map[键类型]interface{}?
      }
      return `` // 处理其他类型
  }
  return ``
}

请注意,我只提供了代码的翻译部分,不包括其他内容。

英文:

For example if I have an interface{} value that originally a map[string]map[int64][]int64 or any other kind of map, how to get the key type of the map? or more precise, how to convert it to 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 ``
}

答案1

得分: 6

获取键类型很容易:

reflect.TypeOf(any).Key()

要完成整个转换,你需要创建一个类型为map[keyType]interface{}的映射值,然后将值复制过去。下面是一个可以实现这个功能的示例:

package main

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

func InterfaceMap(i interface{}) (interface{}, error) {
	// 获取类型
	t := reflect.TypeOf(i)

	switch t.Kind() {
	case reflect.Map:
		// 获取提供的映射值
		v := reflect.ValueOf(i)

		// 使用interface{}创建reflect.Type的“唯一”方法
		it := reflect.TypeOf((*interface{})(nil)).Elem()

		// 创建特定类型的映射。键类型为t.Key(),元素类型为it
		m := reflect.MakeMap(reflect.MapOf(t.Key(), it))

		// 将值复制到新的映射中
		for _, mk := range v.MapKeys() {
			m.SetMapIndex(mk, v.MapIndex(mk))
		}

		return m.Interface(), nil

	}

	return nil, errors.New("不支持的类型")
}

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}

Playground: http://play.golang.org/p/tJTapGAs2b

英文:

Getting the Key type is easy:

reflect.TypeOf(any).Key()

To make the entire conversion, you need to create a map value of type map[keyType]interface{} and then copy the values over. Below is a working example how this can be done:

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{}))
}

Output:

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

Playground: http://play.golang.org/p/tJTapGAs2b

huangapple
  • 本文由 发表于 2015年1月23日 16:14:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/28105598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定