donglizuo8892 2015-03-15 00:19
浏览 1411
已采纳

如何从golang的Redis键值存储中获取列表值?

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

  • 写回答

1条回答 默认 最新

  • duanquanzhi5560 2015-03-15 02:59
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?