Redis驱动
推荐包
https://github.com/astaxie/go...安装
go get -u github.com/astaxie/goredis
上述驱动的源码地址,源码是最好的文档! 很香~~~
https://github.com/astaxie/goredis/blob/master/redis.go
实例代码
package main
import (
"fmt"
"github.com/astaxie/goredis"
)
func main () {
var client goredis.Client
client.Addr="127.0.0.1:6379"
err:=client.Set("test",[]byte("hello world"))
if err!=nil {
panic(err)
}else {
fmt.Println("设置成功")
}
res,err:=client.Get("test")
if err!=nil {
panic(err)
}else {
fmt.Println("%T",res)
fmt.Println(string(res))
}
ts,err:=client.Setnx("test01",[]byte("lamp"))
if err !=nil {
panic(err)
}else {
fmt.Println(ts)
}
f:=make(map[string]interface{})
f["name"]="zhangsan"
f["age"]=23
f["sex"]="nam"
err=client.Hmset("test_hash",f)
if err!=nil {
panic(err)
}else {
fmt.Println("hash数据设置成功")
}
name,err:=client.Hget("test_hash","name")
if err!=nil {
panic(err)
}else {
fmt.Printf("%T\n",name) // []uint8 字节型(字符类型):byte(uint8别名)
fmt.Println("hash_name:",string(name))
}
}
有疑问加站长微信联系(非本文作者)