1:安装redigo
go get github.com/garyburd/redigo/redis
2:import redigo
import (
"github.com/garyburd/redigo/redis"
)
3:连接redis
c, err := redis.Dial("tcp", "192.168.2.225:6379")
if err != nil {
fmt.Println("connect to redis err", err.Error())
return
}
defer c.Close()
4:命令执行
n,err := c.Do("hset","key","field","value") //写
result,err := redis.Values(c.Do("hgetall","key"))//读
5:hash常见命令
hset(key, field, value):向名称为key的hash中添加元素field
hget(key, field):返回名称为key的hash中field对应的value
hmget(key, (fields)):返回名称为key的hash中field i对应的value
hmset(key, (fields)):向名称为key的hash中添加元素field
hincrby(key, field, integer):将名称为key的hash中field的value增加integer
hexists(key, field):名称为key的hash中是否存在键为field的域
hdel(key, field):删除名称为key的hash中键为field的域
hlen(key):返回名称为key的hash中元素个数
hkeys(key):返回名称为key的hash中所有键
hvals(key):返回名称为key的hash中所有键对应的value
hgetall(key):返回名称为key的hash中所有的键(field)及其对应的value
6:示例
6.1:hset示例
_, err = c.Do("hset", "myhash", "bike1", "mobike")
if err != nil {
fmt.Println("haset failed", err.Error())
}
6.2:hget示例
res, err := c.Do("hget", "myhash", "bike1")
fmt.Println(reflect.TypeOf(res))
if err != nil {
fmt.Println("hget failed", err.Error())
} else {
fmt.Printf("hget value :%s\n", res.([]byte))
}
6.3:hmset/hmget
_, err = c.Do("hmset", "myhash", "bike2", "bluegogo", "bike3", "xiaoming", "bike4", "xiaolan")
if err != nil {
fmt.Println("hmset error", err.Error())
} else {
value, err := redis.Values(c.Do("hmget", "myhash", "bike1", "bike2", "bike3", "bike4"))
if err != nil {
fmt.Println("hmget failed", err.Error())
} else {
fmt.Printf("hmget myhash's element :")
for _, v := range value {
fmt.Printf("%s ", v.([]byte))
}
fmt.Printf("\n")
}
}
6.4:hincrby
_, err = c.Do("hmset", "myhash", "bike2", "bluegogo", "bike3", "xiaoming", "bike4", "xiaolan")
if err != nil {
fmt.Println("hmset error", err.Error())
} else {
value, err := redis.Values(c.Do("hmget", "myhash", "bike1", "bike2", "bike3", "bike4"))
if err != nil {
fmt.Println("hmget failed", err.Error())
} else {
fmt.Printf("hmget myhash's element :")
for _, v := range value {
fmt.Printf("%s ", v.([]byte))
}
fmt.Printf("\n")
}
}
6.5:hexists
isExist, err := c.Do("hexists", "myhash", "tmpnum")
if err != nil {
fmt.Println("hexist failed", err.Error())
} else {
fmt.Println("exist or not:", isExist)
}
6.6:hlen
ilen, err := c.Do("hlen", "myhash")
if err != nil {
fmt.Println("hlen failed", err.Error())
} else {
fmt.Println("myhash's len is :", ilen)
}
6.7:hkeys
resKeys, err := redis.Values(c.Do("hkeys", "myhash"))
if err != nil {
fmt.Println("hkeys failed", err.Error())
} else {
fmt.Printf("myhash's keys is :")
for _, v := range resKeys {
fmt.Printf("%s ", v.([]byte))
}
fmt.Println()
}
6.8:hvals
resValues, err := redis.Values(c.Do("hvals", "myhash"))
if err != nil {
fmt.Println("hvals failed", err.Error())
} else {
fmt.Printf("myhash's values is:")
for _, v := range resValues {
fmt.Printf("%s ", v.([]byte))
}
fmt.Println()
}
6.9:hdel
_, err = c.Do("HDEL", "myhash", "tmpnum")
if err != nil {
fmt.Println("hdel failed", err.Error())
}
6.10:hgetall
result, err := redis.Values(c.Do("hgetall", "myhash"))
if err != nil {
fmt.Println("hgetall failed", err.Error())
} else {
fmt.Printf("all keys and values are:")
for _, v := range result {
fmt.Printf("%s ", v.([]byte))
}
fmt.Println()
}
完整代码路径