We can query the length of the map with the builtin len()
function. So we can create a big-enough slice to hold all the keys and all the values.
After that it's enough to iterate over the map only once, and you can fill the first half of the slice with the keys, and the second half of the slice with the values.
This is as efficient as it can get: no builtin append()
is called, and we iterate over the map only once.
Let's see an example function that will receive the keys and values. This one just prints all of them:
func pairs(keysvalues ...string) {
for _, s := range keysvalues {
fmt.Print(s, ", ")
}
}
And the code that creates the keysvalues
slice:
m := map[string]string{
"a": "A",
"b": "B",
"c": "C",
}
count := len(m)
all := make([]string, count*2)
i := 0
for k, v := range m {
all[i], all[count+i] = k, v
i++
}
Once you have the all
slice, you can call the pairs()
function like this:
pairs(all...)
Note though that the iteration order over a map is not deterministic, it may change from iteration to iteration.
Example output (try it on the Go Playground):
a, b, c, A, B, C,
Note:
In your question you indicated that all keys come first and then follow all the values:
func(key1, key2, ..., value1, value2, ...)
In practice this is rare and often the key-value pairs are listed. It's much easier to work/process this variant:
func pairs(key1, value1, key2, value2, ... keyn, valuen)
If we would want to produce this key-value pair list, very similarly it would look like this:
count := len(m)
all := make([]string, count*2)
i := 0
for k, v := range m {
all[i], all[i+1] = k, v
i += 2
}