HKEYS
- 如果hash表存在且有字段,则返回所有字段名
- 如果key不存在,则返回一个空列表
Command
$ redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> hset snake name kobe
(integer) 1
127.0.0.1:6379> hset snake number 24
(integer) 1
127.0.0.1:6379> hkeys snake
1) "name"
2) "number"
127.0.0.1:6379> del snake
(integer) 1
127.0.0.1:6379> hkeys snake
(empty list or set)
Code
func Hkeys(c redis.Conn) {
// If hash table is existing and has field(s), will return the name of all the fields.
c.Do("HSET", "snake", "name", "kobe")
c.Do("HSET", "snake", "number", "24")
fields, err := redis.Strings(c.Do("HKEYS", "snake"))
if err != nil {
colorlog.Error(err.Error())
return
}
for i, v := range fields {
fmt.Println("field", i, "is:", v)
}
// If key doesn't exist, will return an empty list.
c.Do("DEL", "snake")
fields, err = redis.Strings(c.Do("HKEYS", "snake"))
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("If key doesn't exist, will return an empty list. List's length is:", len(fields))
}
Output
$ go run main.go
field 0 is: name
field 1 is: number
If key doesn't exist, will return an empty list. List's length is: 0
网友评论