简体   繁体   中英

How to take all keys and values of a map and pass as args to a function in golang?

I encountered such a situation:

Got a map first, and its size is unknown .

Eg: myMap map[string]string

And then, I would take all the keys and values of the map as a function's args

eg func(key1, key2, ..., value1, value2, ...)

An example func: https://godoc.org/github.com/garyburd/redigo/redis#Script.Do

How to achieve this in go?

As efficient as impossible.

Any help will be appreciated :)

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
}

For example,

package main

import "fmt"

func fn(args ...string) { fmt.Println(args) }

func main() {
    m := map[string]string{"k1": "v1", "k2": "v2"}
    args := make([]string, 0, 2*len(m))
    for key, value := range m {
        args = append(args, key)
        args = append(args, value)
    }
    fmt.Println(args)
    fn(args...)
}

Output:

[k1 v1 k2 v2]
[k1 v1 k2 v2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM