I read with interest a question about how to create a slice of the keys of a map in Go (using a range loop), but how would you write a function that would do this for a map of any type? The map value type is a particular nuisance since it doesn’t even affect the type of the resulting slice.
For instance, could I write a function that takes as argument a map[int]T (where T is any type) and returns a []int (integer slice) of the map’s keys? And if that’s possible, can a function be made to operate on a map[T1]T2 (where T1 and T2 are any type) and return a []T1 slice? That is, what do I replace the question marks with in the below code? I’m guessing I need an interface but I’m a bit of a beginner with those.
func keys(m map[?]?) []? {
var s []?
k := range m {
s = append(s, k)
}
return s
}