1.导入Redis包
在命令行输入
go get github.com/astaxie/goredis
2.测试实例
启动redis测试
package main
import (
"fmt"
"github.com/astaxie/goredis"
)
func main(){
client:=goredis.Client{}
client.Addr="127.0.0.1:6379"
//设置key
err:=client.Set("test",[]byte("goredis"))
if err!=nil{
panic(err)
}
//获取key
res,err:=client.Get("test")
if err!=nil{
panic(err)
}else{
fmt.Println(string(res))
}
/*fmt.Println(client.Db)
fmt.Println(client.Password)*/
//设置map
map1:=make(map[string]string)
map1["username"]="root"
map1["password"]="123456"
err1:=client.Hmset("map1",map1)
if err!=nil{
panic(err1)
}
//获取map1类型
map11,err1:=client.Type("map1")
if err1!=nil{
panic(err1)
}else{
fmt.Println(string(map11))
}
err=client.Hgetall("map1",map1)
if err!=nil{
panic(err)
}
//Zdd
res3,err:=client.Zadd("z",[]byte("z11"),12.3)
if err!=nil{
panic(err)
}else{
fmt.Println(res3)
}
}