SMEMBERS

2020-04-16  本文已影响0人  NotFoundW

SMEMBERS

  1. 如果集合中存在元素,则返回所有元素
  2. 如果key不存在,则返回空列表

Command

$ redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> sadd s1 1 2 ff
(integer) 3
127.0.0.1:6379> smembers s1
1) "1"
2) "2"
3) "ff"
127.0.0.1:6379> smembers fakeSet
(empty list or set)

Code

func smembers(c redis.Conn) {
    defer c.Do("DEL", "s1")
    c.Do("SADD", "s1", 1, 2, "ff")
    members, err := redis.Strings(c.Do("SMEMBERS", "s1"))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("members are as following:")
    for _, v := range members {
        fmt.Println(v)
    }
    //  If key doesn't exist, will return empty list.
    members, err = redis.Strings(c.Do("SMEMBERS", "fakeSet"))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("the number of members of fakeSet is:", len(members))
}

Output

$ go run main.go 
members are as following:
1
2
ff
the number of members of fakeSet is: 0
上一篇下一篇

猜你喜欢

热点阅读