du521521521
2019-04-06 21:17
浏览 495

如何在go-redis包中将键数组传递给MGET函数?

I'm using go-redis package (also tried redigo) and want to get multiple values, it seems I must use MGET() function but function input is like (keys ...string) , is there any way so I can build an array of keys and pass to the function or any other hacky way to do this? in my case,the number of keys(and also keys) are not fixed and want to handle it dynamically

图片转代码服务由CSDN问答提供 功能建议

我正在使用go-redis包(也尝试过redigo),并且想要获取多个值,看来我必须 使用 MGET()函数,但是函数输入就像(keys ... string)一样,有什么方法可以构建键数组并传递给该函数,或者 还有其他方法吗? 就我而言,键(以及键)的数量不是固定的,而是想动态地处理它

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dragon5006 2019-04-06 21:22
    已采纳

    Create a slice of strings with the keys. Call the function using variadic syntax:

    var keys []string
    keys = append(keys, "foo")
    keys = append(keys, "bar")
    sc := client.MGet(keys...)
    

    The same approach works with Redigo, but use a slice of interface instead of a slice of strings:

    var keys []interface{}
    keys = append(keys, "foo")
    keys = append(keys, "bar")
    r, err := conn.Do("MGET", keys...)
    
    打赏 评论

相关推荐 更多相似问题