dqwolwst50489 2019-06-20 14:14
浏览 4
已采纳

在包含地图[int] * somepointer的接口{}上循环

I have to deal with a lot of maps with int keys which contain pointers to different datatypes.

I need a function (and not 10 functions for each map type) to range over those maps and get the maximum and minimum key values.

  • 写回答

2条回答 默认 最新

  • duanhui3759 2019-06-20 15:03
    关注

    Use the reflect package to operate on maps with integer keys and arbitrary value types:

    func getMaxKey(inout interface{}) int {
        keys := reflect.ValueOf(inout).MapKeys()
        if len(keys) == 0 {
            return 0
        }
        max := keys[0].Int()
        for _, key := range keys[1:] {
            n := key.Int()
            if n > max {
                max = n
            }
        }
        return int(max)
    }
    

    Run it on the playground.

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

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教