drll85318 2014-06-05 14:53 采纳率: 100%
浏览 553

将Redigo Pipeline结果转换为字符串

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

  • 写回答

2条回答 默认 最新

  • dongshuan8722 2014-06-05 15:08
    关注

    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]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)