douni1396 2016-08-22 03:57
浏览 42
已采纳

如何获取地图钥匙

I have a function named Keys() to get all the keys of a map, here is the code:

func main() {
    m2 := map[int]interface{}{
        2:"string",
        3:"int",
    }
    fmt.Println(Keys(m2))
}
func Keys(m map[interface{}]interface{}) (keys []interface{}) {
    for k := range m {
        keys = append(keys, k)
    }
    return keys
}

But I got

cannot use m2 (type map[int]interface {}) as type map[interface {}]interface {} in argument to Keys

Does Go support generics and how should I fix my code?

  • 写回答

2条回答 默认 最新

  • dtwbp26022 2016-08-22 04:40
    关注

    1- Golang is strongly typed language, So the map[int]interface{} is not compatible with map[interface{}]interface{}.
    int is different type than interface{}, and see: Go: What's the meaning of interface{}?

    2- No, Golang doesn't support generics, and this is very good, because it makes language simple and fast.


    You have some options:

    If you don't want to change the type of map used:
    1- You may edit the function to: func Keys(m map[int]interface{}) []int, like this working sample code:

    package main
    
    import "fmt"
    
    func main() {
        m2 := map[int]interface{}{
            2: "string",
            3: "int",
        }
        fmt.Println(Keys(m2))
    }
    
    func Keys(m map[int]interface{}) []int {
        keys := make([]int, len(m))
        i := 0
        for k := range m {
            keys[i] = k
            i++
        }
        return keys
    }
    

    output ( may not be in order):

    [2 3]
    

    2- Or you may edit the function to: func Keys(m map[int]interface{}) []interface{}, like this working sample code:

    package main
    
    import "fmt"
    
    func main() {
        m2 := map[int]interface{}{
            2: "string",
            3: "int",
        }
        fmt.Println(Keys(m2))
    }
    
    func Keys(m map[int]interface{}) []interface{} {
        keys := make([]interface{}, len(m))
        i := 0
        for k := range m {
            keys[i] = k
            i++
        }
        return keys
    }
    

    output ( may not be in order):

    [2 3]
    

    If you don't want to change the Keys function used:
    3- You may edit the map to: map[interface{}]interface{}, like this working sample code:

    package main
    
    import "fmt"
    
    func main() {
        m2 := map[interface{}]interface{}{
            2: "string",
            3: "int",
        }
        fmt.Println(Keys(m2))
    }
    
    func Keys(m map[interface{}]interface{}) []interface{} {
        keys := make([]interface{}, len(m))
        i := 0
        for k := range m {
            keys[i] = k
            i++
        }
        return keys
    }
    

    4- Also you may use reflect package for some use cases, but with the performance (speed) penalty.
    And See: The Laws of Reflection

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!