HSET
- 如果字段对于hash表来说是新的,那么执行成功后返回1
- 如果字段已经存在与hash表,那么执行成功后会覆盖旧值,并返回0
Command
$ redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> hset snake name kobe
(integer) 1
127.0.0.1:6379> hset snake name mamba
(integer) 0
Code
func Hset(c redis.Conn) {
defer c.Do("DEL", "snake")
// If field is new for hash table and set successfully, will return 1.
returnCode, err := c.Do("HSET", "snake", "name", "kobe")
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("Hset successfully, return:", returnCode)
// If field is existing in hash table, will overwrite the old value and return 0.
returnCode, err = c.Do("HSET", "snake", "name", "mamba")
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("Hset overwrite successfully, return:", returnCode)
}
Output
$ go run main.go
Hset successfully, return: 1
Hset overwrite successfully, return: 0
网友评论