HMGET:传递参数时返回空结果。

huangapple go评论71阅读模式
英文:

HMGET: Empty result when passing params

问题

使用redigo,我正在尝试使用HMGET命令。我将一个字符串切片作为参数传递给field。但是它不起作用,返回的结果为空。

以下是代码示例:

func HMGET(c redis.Conn, field []string)(){
    if err := c.Send("HMGET", HashName, field); err != nil {
        return nil, err
    }
    if err := c.Flush(); err != nil {
        return nil, err
    }
    rval, err := c.Receive()
    if err != nil {
        return nil, err
    }
    return rval, nil
}

这段代码不起作用。

c.Send("HMGET", r.HashName, "1", "2", "3")

有什么建议吗?为什么当field作为参数传递时不起作用?

英文:

Using redigo, I'm trying to use HMGET. I'm passing a string slice as param in field. It is not working, returning empty result.

func HMGET(c redis.Conn, field []string)(){
        if err := c.Send("HMGET", HashName, field); err != nil {
            return nil, err
        }
        if err := c.Flush(); err != nil {
            return nil, err
        }
        rval, err := c.Receive()
        if err != nil {
            return nil, err
        }
        return rval, nil
}

This is working

c.Send("HMGET", r.HashName, "1", "2", "3")

Any suggestions why field when passed as param is not working?

答案1

得分: 2

你发送的是 HMGET r.HashName [1 2 3]

将参数分开或将它们添加到同一个切片中,并将该切片扩展为可变参数。由于你使用的是 []string 类型,你还需要将其转换为 []interface{} 类型:

func HMGET(c redis.Conn, field []string) {
    args := make([]interface{}, len(field)+1)
    args[0] = HashName
    for i, v := range field {
        args[i+1] = v
    }
    
    if err := c.Send("HMGET", args...); err != nil {
        return nil, err
    }
    //////
}
英文:

What you're sending is HMGET r.HashName [1 2 3]

Separate the arguments or add them the same slice and expand that slice as the variadic parameter. Since you're using type []string you will need to convert that to an []interface{} type as well:

func HMGET(c redis.Conn, field []string) {
    args := make([]interface{}, len(field)+1)
    args[0] = HashName
    for i, v := range field {
    	args[i+1] = v
    }
    
    if err := c.Send("HMGET", args...); err != nil {
    	return nil, err
    }
    //////

huangapple
  • 本文由 发表于 2016年12月5日 21:39:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/40975589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定