我试图使用HMSET设置一个新的hash,其中包含两个字符串字段,id和content。
我可以通过redis-cli轻松地使用SET i 0初始化in计数器,然后使用HMSET test id hey content herro创建一个新的哈希,并使用HMGET test id content获得这两个字段,生成1) hey 2) herro。
不幸的是,我无法在去-雷迪斯,特别是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(),一切都很好。我得到:
对持有错误值的键的操作
我真的不明白为什么,因为我设法使它在redis-cli中以同样的方式工作。
HMSet被定义为func (c *Client) HMSet(key string, fields map[string]interface{}) *StatusCmd。
我无法在网上找到任何使用Go-Redis演示这个用例的例子。
我做错什么了?
发布于 2017-05-02 20:47:04
您将访问相同的密钥"i"两次--一次是调用SET时的字符串,然后是调用HMSET时的哈希。
您所得到的错误只是对字符串拒绝HMSET,这是一个无效的操作。
顺便说一句,相反,对任何类型的redis的调用集只会写一个字符串而不是那个值,所以要小心。
https://stackoverflow.com/questions/43746853
复制相似问题