gpt4 book ai didi

redis - redigo,SMEMBERS,如何获取字符串

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

我是redigo从 Go 连接到 redis 数据库。 如何将 []interface {}{[]byte{} []byte{}} 类型转换为一组字符串?在这种情况下,我会喜欢获取两个字符串 HelloWorld

package main

import (
"fmt"
"github.com/garyburd/redigo/redis"
)

func main() {
c, err := redis.Dial("tcp", ":6379")
defer c.Close()
if err != nil {
fmt.Println(err)
}
c.Send("SADD", "myset", "Hello")
c.Send("SADD", "myset", "World")
c.Flush()
c.Receive()
c.Receive()

err = c.Send("SMEMBERS", "myset")
if err != nil {
fmt.Println(err)
}
c.Flush()
// both give the same return value!?!?
// reply, err := c.Receive()
reply, err := redis.MultiBulk(c.Receive())
if err != nil {
fmt.Println(err)
}
fmt.Printf("%#v\n", reply)
// $ go run main.go
// []interface {}{[]byte{0x57, 0x6f, 0x72, 0x6c, 0x64}, []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f}}
// How do I get 'Hello' and 'World' from this data?
}

最佳答案

module source code中查找

// String is a helper that converts a Redis reply to a string. 
//
// Reply type Result
// integer format as decimal string
// bulk return reply as string
// string return as is
// nil return error ErrNil
// other return error
func String(v interface{}, err error) (string, error) {

redis.String 会将(v interface{}, err error)转换成(string, error)

reply, err := redis.MultiBulk(c.Receive())

替换为

s, err := redis.String(redis.MultiBulk(c.Receive()))

关于redis - redigo,SMEMBERS,如何获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12629801/

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