I'm using the redigo
redis library, and checking if a key is already present in a redis set
. I'm using the redis command SISMEMBER
via the redigo's Do
method which returns an interface. It is the response of the command being executed using Do
. In case of SISMEMBER
command, the response in a 1
or 0
. and in this particular case, the response is 0
as the given key is not present in this set. however reflect.DeepEqual(resp, 0)
is returning false
when true
is expected. Is this not the correct way to use DeepEqual
function?
import (
"fmt"
"reflect"
"github.com/garyburd/redigo/redis"
)
func main() {
conn, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println(err.Error())
return
}
defer conn.Close()
resp, err := conn.Do("SISMEMBER", "mySet", "Hello")
if reflect.DeepEqual(resp, 0) {
fmt.Println("record doesn't exist")
}
}
Instead of over complicate your code, you can use some build-in functions of redigo to convert the response to int
and afterward check if the key is or is not in redis.
resp, err := redis.Int(conn.Do("SISMEMBER", "mySet", "Hello"))
if err != nil {
// some error handler
}
if resp == 0 {
fmt.Println("record doesn't exist")
}
fmt.Printf("ret type: %T, ret: %v
", resp, resp)
when I did check the type of the resp
interface, i found that it is int64
, so casting the 0
appropriately, like below, helped.
reflect.DeepEqual(resp, int64(0))