简体   繁体   中英

redigo, SMEMBERS, how to get strings

I am redigo to connect from Go to a redis database. How can I convert a type of []interface {}{[]byte{} []byte{}} to a set of strings? In this case I'd like to get the two strings Hello and World .

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?
}

Look in 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 will convert (v interface{}, err error) in (string, error)

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

replace with

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

Looking at the source code for the module, you can see the type signature returned from Receive will be:

func (c *conn) Receive() (reply interface{}, err error)

and in your case, you're using MultiBulk :

func MultiBulk(v interface{}, err error) ([]interface{}, error)

This gives a reply of multiple interface{} 's in a slice: []interface{}

Before an untyped interface{} you have to assert its type like so:

x.(T)

Where T is a type (eg, int , string etc.)

In your case, you have a slice of interfaces (type: []interface{} ) so, if you want a string , you need to first assert that each one has type []bytes, and then cast them to a string eg:

for _, x := range reply {
    var v, ok = x.([]byte)
    if ok {
        fmt.Println(string(v))
    }
}

Here's an example: http://play.golang.org/p/ZifbbZxEeJ

You can also use a type switch to check what kind of data you got back:

http://golang.org/ref/spec#Type_switches

for _, y := range reply {
    switch i := y.(type) {
    case nil:
        printString("x is nil")
    case int:
        printInt(i)  // i is an int
    etc...
    }
}

Or, as someone mentioned, use the built in redis.String etc. methods which will check and convert them for you.

I think the key is, each one needs to be converted , you can't just do them as a chunk (unless you write a method to do so!).

To make some advertisement for my own product: just take a look at http://cgl.tideland.biz . There you'll also find my Redis client. It supports each command as well as multi-commands and pub/sub. As a return value you get a result set which allows you a convenient access to single or multiple return values or hashes together with methods for conversion into the native Go types.

Since redis.MultiBulk() now is deprecated, it might be a good way to use redis.Values() and convert the result into String :

import "github.com/gomodule/redigo/redis"

type RedisClient struct {
    Conn      redis.Conn
}

func (r *RedisClient) SMEMBERS(key string) interface{} {
    tmp, err := redis.Values(r.Conn.Do("smembers", key))
    if err != nil {
        fmt.Println(err)
        return nil
    }
    res := make([]string, 0)
    for _, v := range tmp {
        res = append(res, string(v.([]byte)))
    }
    return res
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM