8

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?
}
topskip
  • 16,207
  • 15
  • 67
  • 99

3 Answers3

8

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()))
Max
  • 6,286
  • 5
  • 44
  • 86
  • 2
    I get the following error: `redigo: unexpected type for String, got type []interface {}` from your line above... – topskip Sep 28 '12 at 05:43
  • @topskip: Probably not necessary to say this since this question already have an answer, but the unexecpted type error is probably because you are inserting a slice of interfaces instead of just the interface. If you had enumerated through the slice of interfaces and used redis.String it would probably work fine. – implmentor Mar 15 '13 at 13:55
4

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

minikomi
  • 8,363
  • 3
  • 44
  • 51
1

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
}