摘要:Go操作Redis - 常见-youmen - 博客园
mget获取数据:
package main
import (
"fmt"
"github.com/garyburd/redigo/redis"
)
func main() {
conn,err := redis.Dial("tcp","121.36.43.223:6379")
if err != nil{
fmt.Println("connect redis error:",err)
return
}
defer conn.Close()
_,err = conn.Do("MSET","name","youmen","age","22")
if err != nil{
fmt.Println("redis mset error:",err)
}
res,err := redis.Strings(conn.Do("MGET","name","age"))
if err != nil{
fmt.Println("redis get error",err)
} else {
fmt.Printf("MGET name: %s \n", res)
fmt.Println(len(res))
}
}
hash操作:
package main
import (
"fmt"
"github.com/garyburd/redigo/redis"
"reflect"
)
func main() {
conn,err := redis.Dial("tcp","121.36.43.223:6379")
if err != nil{
fmt.Println("connect redis error:",err)
return
}
defer conn.Close()
_, err = conn.Do("HSET", "student","name", "wd","age",22)
if err != nil {
fmt.Println("redis mset error:", err)
}
res, err := redis.Int64(conn.Do("HGET", "student","age"))
if err != nil {
fmt.Println("redis HGET error:", err)
} else {
res_type := reflect.TypeOf(res)
fmt.Printf("res type : %s \n", res_type)
fmt.Printf("res : %d \n", res)
}
}
|