我设法管道多个HGETALL命令,但我无法将它们转换为字符串.
我的示例代码是这样的:
// 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)
Run Code Online (Sandbox Code Playgroud)
只要你很舒服地显示非字符串结果就很好..我得到的是这样的:
[[[116 105 116 108 101] [104 105]] [[116 105 116 108 101] [104 101 108 108 111]]]
Run Code Online (Sandbox Code Playgroud)
但我需要的是:
"title" "hi" "title" "hello"
Run Code Online (Sandbox Code Playgroud)
我也尝试过以下和其他组合:
result, _ := redis.Strings(pipe_prox, err)
log.Println(pipe_prox)
Run Code Online (Sandbox Code Playgroud)
但我得到的只是: []
我应该注意它适用于多个HGET 键值命令,但这不是我需要的.
我究竟做错了什么?我应该如何将"数字地图"转换为字符串?
谢谢你的帮助
每个都HGETALL返回它自己的一系列值,需要转换为字符串,管道返回一系列值.首先使用泛型redis.Values来分解这个外部结构然后你可以解析内部切片.
// 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)
}
Run Code Online (Sandbox Code Playgroud)
打印:
[title hi]
[title hello]
Run Code Online (Sandbox Code Playgroud)