简体   繁体   中英

How to get a value which is a list from redis key-value store in golang?

I am writing a function in golang to get the value from redis db by passing the key. The value is a list. I am using 'GET' redis command to get the value. But it is giving me error.

You can find below the code,

func GetValue(key string) []string {
    var value []string
    var err error
    value, err = redis.Strings(conn.Do("GET", key))

    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(value)
    return value
}

func RetrieveValue() {
    keyType, _ := conn.Do("TYPE", recentItemKey)
    fmt.Println("Type", keyType)

    var results []string
    results = GetValue(recentItemKey)

    for _, val := range results {
        fmt.Println(val)
    }
}

And the output is here,

Type list
2015/03/14 19:09:12 WRONGTYPE Operation against a key holding the wrong kind of value
exit status 1

Version

Go 1.4.2
Redis-2.8.19

Redis Go Library

github.com/garyburd/redigo/redis

Could anyone help me on this.? Thank you

Use LRANGE to get the elements of a list:

func GetValues(key string) []string {
  value, err := redis.Strings(conn.Do("LRANGE", key, 0, -1))
  if err != nil {
    log.Fatal(err)
  }
  return value
}

The GET command gets the value of a string key. The GET command does not work on list keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM