Ram
Ram

Reputation: 1193

How to do Redis (redigo) lpop in golang

I need to do a simple lpop from a redis queue. In go lang If I use a blocking pop using blpop then the foll code works

reply, err := redis.Strings(conn.Do("BLPOP", key, 1))
        if err == nil {         
           fmt.Println(reply[1])

        // do something with string reply[1]

       }

But I do not want to block. I just need to end when the queue is empty. So how can I do that If I do redis.Strings(conn.Do("LPOP", key)) ie change BLPOP to LPOP and I get a redigo error

ERROR = redigo: unexpected type for Strings, got type []uint8 

Upvotes: 1

Views: 3104

Answers (2)

Thundercat
Thundercat

Reputation: 121169

The BLPOP command returns a two element array where the first element is the key and the second value is the popped element.

The LPOP command returns the popped element.

Use the String helper function to get the popped element:

reply, err := redis.String(conn.Do("LPOP", key))
        if err == nil {         
           fmt.Println(reply)
        // do something with string reply
       }

Upvotes: 0

Ram
Ram

Reputation: 1193

I can get the lpop'ed value using redis.String()

so this works

reply, err := redis.String(conn.Do("LPOP", key))
    if err == nil { 
     fmt.Printf("REPLY= %s\n", reply)
      //do something 

Upvotes: 2

Related Questions