英文:
redigo: read redis hash that has variable keys
问题
我需要从redigo中读取一个Redis哈希表。这个哈希表的键是可变的。这会导致一个问题,因为ScanStruct要求我事先知道这些键,这样我才能将其放入一个结构体中,并将HGETALL的结果解包到该结构体中。
有没有一种方法可以解析具有未知键的redigo HGETALL结果?它不一定要使用ScanStruct(甚至不一定要使用redigo),只要我能够在Go中访问结果即可。
英文:
I need to read a redis hash from redigo. This hash has variable keys. This causes a problem because ScanStruct requires me to know those keys beforehand, so I can put it in a struct and unpack the HGETALL result into that struct.
Is there a way to parse a redigo HGETALL result that has unknown keys? It does not have to be with ScanStruct (or even with redigo), as long as I can access the result from within go.
答案1
得分: 6
ScanStruct
只是一个方便的工具,用于将已知的结构体映射到Redis哈希表。
使用redis.StringMap
辅助函数获取map[string]string
。
在Redis中,一切都是字符串,而redigo库会为您转换值。您可以根据需要轻松地从它们的字符串表示中转换值。如果您想要原始字节而不进行第一次字符串转换,可以使用redis.Values
,它将返回交替的键和值的[]interface{}
。
英文:
ScanStruct
is just a convenience for when you're mapping a known struct to a redis hash.
Use the redis.StringMap
helper function to get a map[string]string
.
Everything is redis is a string, and the redigo library converts the values for you. You can easily convert the values as needed from their string representations. If you want the raw bytes without the first string conversion, you can use redis.Values
, which will return alternating keys and values in a []interface{}
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论