简体   繁体   中英

Reading Redis key-value which is JSON string using redigo

I am trying to read Redis Key-val in Go. Key is string and value is JSON string. Eg- Key=

discov_32161296

and Value as Json string=

"{\"10283\":true,\"11064\":true,\"15123\":true,\"15447\":true,\"15926\":true,\"16530\":true,\"16537\":true,\"16799\":true,\"17088\":true,\"17249\":true,\"18501\":true,\"18529\":true,\"18601\":true,\"3044\":true,\"3687\":true,\"4926\":true,\"5483\":true,\"6\":true,\"6675\":true,\"8332\":true,\"8336\":true,\"8674\":true}"

Getting below error while reading in Go

redis.Values err redigo: unexpected type for Values, got type []uint8

Here's my code :

uIDDiscoveryOffer := fmt.Sprintf("%s_%d", "discov", uid)
opDataStr, err := redis.String(redis.Values(con.Do("GET", uIDDiscoveryOffer)))
    if err != nil || err != redis.ErrNil {
        utils.Log1("readCacheTxnByUID-Disc-redis.Values-err", fmt.Sprint("redis.Values err : ", uidDiscoveryOffer, " error: ", err.Error()))
    } else {
         //Some Logic
    }

The Redis GET returns the value of a key. redis.Values() may be used to convert the result of a command that returns multiple items.

Since GET returns a single item, only useredis.String() , you don't need redis.Values() here:

opDataStr, err := redis.String(con.Do("GET", uIDDiscoveryOffer))

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