gpt4 book ai didi

go - 将 Redigo 管道结果转换为字符串

转载 作者:IT王子 更新时间:2023-10-29 01:53:16 25 4
gpt4 key购买 nike

我设法通过管道传输多个 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)

只要您愿意显示非字符串结果就可以了。我得到的是这样的:

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

但我需要的是:

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

我也尝试了以下和其他组合:

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

log.Println(pipe_prox)

但我得到的只是:[]

我应该注意到它适用于多个 HGET key value 命令,但这不是我需要的。

我做错了什么?如何将“数字映射”转换为字符串?

感谢您的帮助

最佳答案

每个 HGETALL 都返回它自己的一系列值,这些值需要转换为字符串,而管道正在返回一系列值。使用通用的 redis.Values 首先分解这个外部结构,然后你可以解析内部 slice 。

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

打印:

[title hi]
[title hello]

关于go - 将 Redigo 管道结果转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24063478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com