Sukeesh
Sukeesh

Reputation: 313

How to pass in key value pair to MSet in redis golang?

MSet in redis-go accepts pairs ...interface{} here

func (c *cmdable) MSet(pairs ...interface{}) *StatusCmd {
    args := make([]interface{}, 1, 1+len(pairs))
    args[0] = "mset"
    args = appendArgs(args, pairs)
    cmd := NewStatusCmd(args...)
    c.process(cmd)
    return cmd
}

Now, I have keys []string mapped to values []int64 to be set in Redis cache. How do I convert them to slice of interfaces and pass them to MSet to make it work?

Upvotes: 2

Views: 4306

Answers (1)

Thundercat
Thundercat

Reputation: 120989

Use a for loop to copy the keys and values to a slice:

var pairs []interface{}
for i := range keys {
  pairs = append(pairs, keys[i], values[i])
}
cmd := c.MSet(pairs...)

Upvotes: 7

Related Questions