HGET

作者: NotFoundW | 来源:发表于2020-04-15 16:25 被阅读0次

HGET

  1. 如果字段存在,返回字段对应的值
  2. 如果字段不存在,返回nil
  3. 如果key不存在,返回nil

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> hget snake name
"kobe"
127.0.0.1:6379> hget snake fakeField
(nil)
127.0.0.1:6379> exists fakeKey
(integer) 0
127.0.0.1:6379> hget fakeKey fakeField
(nil)

Code

func Hget(c redis.Conn) {
    defer c.Do("DEL", "snake")
    //  If field is existing, will return the value of field.
    c.Do("HSET", "snake", "name", "kobe")
    valueOfField, err := redis.String(c.Do("HGET", "snake", "name"))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("Value of field name is:", valueOfField)
    //  If field doesn't exist, will return nil.
    valueOfField, err = redis.String(c.Do("HGET", "snake", "fakeField"))
    if err != nil {
        colorlog.Error(err.Error())
    }
    //  If key doesn't exist, will return nil.
    isExist, _ := c.Do("EXISTS", "fakeKey")
    if isExist == 1 {
        c.Do("DEL", "fakeKey")
    }
    valueOfField, err = redis.String(c.Do("HGET", "fakeKey", "fakeField"))
    if err != nil {
        colorlog.Error(err.Error())
    }
}

Output

$ go run main.go 
Value of field name is: kobe
[ERR]2020/04/15 10:41:49 redigo: nil returned
[ERR]2020/04/15 10:41:49 redigo: nil returned

相关文章

  • HGET

    HGET 如果字段存在,返回字段对应的值 如果字段不存在,返回nil 如果key不存在,返回nil Command...

  • Redis命令Hash(哈希表)教程

    HDEL HEXISTS HGET HGETALL HINCRBY HINCRBYFLOAT HKEYS HLEN...

  • Redis操作--hash(哈希表)

    目录 HDEL HEXISTS HGET HGETALL HINCRBY HINCRBYFLOAT HKEYS H...

  • Redis hash表

    hash表 练习命令使用,具体如下: hset hmset hgetall hkeys hvals hget hm...

  • redis hash

    哈希 特点 1.键值结构key field valuefield 不能相同 重要API hget key fiel...

  • redis 第八讲 Hash

    key-value 模式不变,但 value 是一个键值对 hset / hget hmset / hmget /...

  • redis 07 五大数据类型-hash

    hash还是key value模式,但是该value又是一堆key value hset/hget/hmset/h...

  • Redis ApI

    hash 其内部结构符合对象形式 命令复杂度hget hset hdelo(1)hexistso(1)hincrb...

  • Redis命令:Hashes

    HSET命令可以设置hash中一个field的值: HGET命令可以获取hash中一个field的值: HMSET...

  • redis存储集合

    redis存储集合的方法的hset()和hget()方法,这两个方法是采用哈希表的方式来实现的,在哈希表中给定一个...

网友评论

      本文标题:HGET

      本文链接:https://www.haomeiwen.com/subject/htocvhtx.html