user3717756
user3717756

Reputation:

Convert Redigo Pipeline result to strings

I managed to Pipeline multiple HGETALL commands, but I can't manage to convert them to strings.

My sample code is this:

// Initialize Redis (Redigo) client on port 6379 
//  and default address 127.0.0.1/localhost
client, err := redis.Dial("tcp", ":6379")
  if err != nil {
  panic(err)
}
defer client.Close()

// Initialize Pipeline
client.Send("MULTI")

// Send writes the command to the connection's output buffer
client.Send("HGETALL", "post:1") // Where "post:1" contains " title 'hi' "

client.Send("HGETALL", "post:2") // Where "post:1" contains " title 'hello' "

// Execute the Pipeline
pipe_prox, err := client.Do("EXEC")

if err != nil {
  panic(err)
}

log.Println(pipe_prox)

It is fine as long as you're comfortable showing non-string results.. What I'm getting is this:

[[[116 105 116 108 101] [104 105]] [[116 105 116 108 101] [104 101 108 108 111]]]

But what I need is:

"title" "hi" "title" "hello"

I've tried the following and other combinations as well:

result, _ := redis.Strings(pipe_prox, err)

log.Println(pipe_prox)

But all I get is: []

I should note that it works with multiple HGET key value commands, but that's not what I need.

What am I doing wrong? How should I do it to convert the "numerical map" to strings?

Thanks for any help

Upvotes: 1

Views: 3287

Answers (2)

johnsneakers
johnsneakers

Reputation: 16

you can do it like this:

pipe_prox, err := redis.Values(client.Do("EXEC"))
for _, v := range pipe_prox.([]interface{}) {
    fmt.Println(v)
}

Upvotes: 0

Mr_Pink
Mr_Pink

Reputation: 109405

Each HGETALL returns it's own series of values, which need to be converted to strings, and the pipeline is returning a series of those. Use the generic redis.Values to break down this outer structure first then you can parse the inner slices.

// Execute the Pipeline
pipe_prox, err := redis.Values(client.Do("EXEC"))

if err != nil {
    panic(err)
}

for _, v := range pipe_prox {
    s, err := redis.Strings(v, nil)
    if err != nil {
        fmt.Println("Not a bulk strings repsonse", err)
    }

    fmt.Println(s)
}

prints:

[title hi]
[title hello]

Upvotes: 5

Related Questions