I'm trying to create a helper function which will take a map with string keys and return a slice of the map keys.
The problem is that I want the function to not care about the type of the map values.
For example:
stringStringMap := map[string]string{
"one": "first",
"two": "second"
}
mapKeys(stringStringMap) // ["one", "two"]
stringIntMap := map[string]int{
"one": 1,
"two": 2,
}
mapKeys(strinIntMap) // ["one", "two"]
It seems that the only way around this problem is to create two similar helpers. Something like this:
func mapKeys(m map[string]string) []string {
...
}
func mapKeys2(m map[string]int) []string {
...
}
But this seems ugly. Is this helper function I'm trying to create possible?If not, is there a good convention I should follow when writing this?