Search code examples
goredisredigo

HMGET: Empty result when passing params


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?


Solution

  • 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
        }
        //////