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