【发布时间】:2017-05-02 20:40:09
【问题描述】:
我正在尝试使用HMSET 设置一个新的hash,其中包含两个字符串字段id 和content。
我可以很容易地通过redis-cli 完成此操作,使用SET i 0 为id 初始化一个计数器,然后使用HMSET test id hey content herro 创建一个新的哈希,并使用HMGET test id content 获取这两个字段,从而得到@987654331 @。
很遗憾,我无法使用 Go-Redis 获得这样的结果,尤其是使用 HMSet。
到目前为止我尝试过
var uid = "0"
err = c.Get("i").Err()
if(err != nil) {
//If the counter is not set, set it to 0
err := c.Set("i", "0", 0).Err()
if(err != nil){
panic(err)
}
} else {
//Else, increment it
counter, err := c.Incr("i").Result()
//Cast it to string
uid = strconv.FormatInt(index, 10)
if(err != nil){
panic(err)
}
//Set new counter
err = c.Set("i", counter, 0).Err()
if( err!= nil ){
panic(err)
}
}
//Init a map[string]interface{}
var m = make(map[string]interface{})
m["id"] = uid
m["content"] = "herro"
hash, err := c.HMSet("i", m).Result()
if(err != nil){
panic(err)
}
fmt.Println(hash)
一切正常,但c.HMSet("i", m).Result()。我明白了:
WRONGTYPE 对持有错误值的键的操作
我真的不明白为什么,因为我设法让它在 redis-cli 中以同样的方式工作。
HMSet 定义为func (c *Client) HMSet(key string, fields map[string]interface{}) *StatusCmd。
我无法在网上找到任何使用 Go-Redis 来说明此用例的示例。
我做错了什么?
【问题讨论】: