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