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

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

Convert interface to its respecting map

问题

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

  1. func Transverse(any interface{}) string {
  2. res := ``
  3. switch any.(type) {
  4. case string:
  5. return ``
  6. case []byte:
  7. return ``
  8. case int, int64, int32:
  9. return ``
  10. case float32, float64:
  11. return ``
  12. case bool:
  13. return ``
  14. case map[int64]interface{}:
  15. return ``
  16. case map[string]interface{}:
  17. return ``
  18. case []interface{}:
  19. return ``
  20. default:
  21. kind := reflect.TypeOf(any).Kind()
  22. switch kind {
  23. case reflect.Map:
  24. // 如何将其转换为map[键类型]interface{}?
  25. }
  26. return `` // 处理其他类型
  27. }
  28. return ``
  29. }

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

英文:

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

  1. func Transverse(any interface{}) string {
  2. res := ``
  3. switch any.(type) {
  4. case string:
  5. return ``
  6. case []byte:
  7. return ``
  8. case int, int64, int32:
  9. return ``
  10. case float32, float64:
  11. return ``
  12. case bool:
  13. return ``
  14. case map[int64]interface{}:
  15. return ``
  16. case map[string]interface{}:
  17. return ``
  18. case []interface{}:
  19. return ``
  20. default:
  21. kind := reflect.TypeOf(any).Kind()
  22. switch kind {
  23. case reflect.Map:
  24. // how to convert it to map[keyType]interface{} ?
  25. }
  26. return `` // handle other type
  27. }
  28. return ``
  29. }

答案1

得分: 6

获取键类型很容易:

  1. reflect.TypeOf(any).Key()

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

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "reflect"
  6. )
  7. func InterfaceMap(i interface{}) (interface{}, error) {
  8. // 获取类型
  9. t := reflect.TypeOf(i)
  10. switch t.Kind() {
  11. case reflect.Map:
  12. // 获取提供的映射值
  13. v := reflect.ValueOf(i)
  14. // 使用interface{}创建reflect.Type的“唯一”方法
  15. it := reflect.TypeOf((*interface{})(nil)).Elem()
  16. // 创建特定类型的映射。键类型为t.Key(),元素类型为it
  17. m := reflect.MakeMap(reflect.MapOf(t.Key(), it))
  18. // 将值复制到新的映射中
  19. for _, mk := range v.MapKeys() {
  20. m.SetMapIndex(mk, v.MapIndex(mk))
  21. }
  22. return m.Interface(), nil
  23. }
  24. return nil, errors.New("不支持的类型")
  25. }
  26. func main() {
  27. foo := make(map[string]int)
  28. foo["anisus"] = 42
  29. bar, err := InterfaceMap(foo)
  30. if err != nil {
  31. panic(err)
  32. }
  33. fmt.Printf("%#v\n", bar.(map[string]interface{}))
  34. }

输出:

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

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

英文:

Getting the Key type is easy:

  1. 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:

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "reflect"
  6. )
  7. func InterfaceMap(i interface{}) (interface{}, error) {
  8. // Get type
  9. t := reflect.TypeOf(i)
  10. switch t.Kind() {
  11. case reflect.Map:
  12. // Get the value of the provided map
  13. v := reflect.ValueOf(i)
  14. // The "only" way of making a reflect.Type with interface{}
  15. it := reflect.TypeOf((*interface{})(nil)).Elem()
  16. // Create the map of the specific type. Key type is t.Key(), and element type is it
  17. m := reflect.MakeMap(reflect.MapOf(t.Key(), it))
  18. // Copy values to new map
  19. for _, mk := range v.MapKeys() {
  20. m.SetMapIndex(mk, v.MapIndex(mk))
  21. }
  22. return m.Interface(), nil
  23. }
  24. return nil, errors.New("Unsupported type")
  25. }
  26. func main() {
  27. foo := make(map[string]int)
  28. foo["anisus"] = 42
  29. bar, err := InterfaceMap(foo)
  30. if err != nil {
  31. panic(err)
  32. }
  33. fmt.Printf("%#v\n", bar.(map[string]interface{}))
  34. }

Output:

  1. 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:

确定