Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redigo, SMEMBERS, how to get strings

Tags:

redis

go

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?
}
like image 531
topskip Avatar asked Sep 27 '12 20:09

topskip


3 Answers

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()))
like image 183
Max Avatar answered Oct 19 '22 22:10

Max


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!).

like image 20
minikomi Avatar answered Oct 19 '22 21:10

minikomi


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
}
like image 1
cookedsteak Avatar answered Oct 19 '22 21:10

cookedsteak