du5910 2018-09-02 13:49
浏览 42
已采纳

redis-cli如何对在HILL或位图上执行GET返回的字符串值进行编码?

Suppose I have a bitmap that I have set using the following commands

setbit key 0 1
setbit key 1 1
setbit key 2 0
setbit key 3 1
setbit key 4 1

When I run get on this key from the redis-cli I get the following result: "\xd8"

But when I do it using a go-redis API I get a different result which is this: � @

To get the above result I have used the following code:

package main

import (
    "fmt"

    "github.com/go-redis/redis"
)

func main() {
    client := redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "",
        DB:       0,
    })

    pong, _ := client.Ping().Result()
    fmt.Println(pong)

    client.SetBit("key", 0, 1)
    client.SetBit("key", 1, 1)
    client.SetBit("key", 2, 0)
    client.SetBit("key", 3, 1)
    client.SetBit("key", 4, 1)  

    res := client.Get("bmk").Val()
    fmt.Println(res)

}

I cannot find it anywhere in the redis docs which kind of encoding the cli uses to produce such results. I would like to convert the results to the same format in my go program as well.

Can someone please tell me what kind of encoding this is and how can I convert the result received from the Redis API to the one obtained by the CLI ?

  • 写回答

1条回答 默认 最新

  • donjd86266 2018-09-02 14:54
    关注

    The redis-cli and the Go program get the same result for GET on some key. The difference that you are observing is how the result value is formatted for output.

    The Go program in the question writes the data as is.

    The redis-cli code for encoding output is here. The function uses typical backslash escapes for newlines, quotes, etc and \x<hex number> for other non-printable bytes.

    Use the fmt package "%q" verb to output data in a similar format:

    fmt.Printf("%q
    ", res)
    

    If you need the exact same format, then a translation of the internal Redis function to Go required.

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

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同