go語言redis使用
阿新 • • 發佈:2020-08-20
package main
import (
"fmt"
"github.com/garyburd/redigo/redis"
)
var redisconnect redis.Conn //重新命名
func init(){
ac,err:=redis.Dial("tcp","127.0.0.1:6379")//與redis建立連線
if err!=nil{
fmt.Println("redis 連線失敗",err)
return
}
fmt.Println("redis 連線成功")
redisconnect=ac
}
func main() {
defer fmt.Println("redis 服務關閉")defer redisconnect.Close()
acString1()
acString2()
acList()
achash()
}
//五種redis資料型別
//hash,列表list,字串,集合set,zset有序集合
//string單值
func acString1(){
_,err:=redisconnect.Do("set","tel",11211)
if err!=nil{
fmt.Println(err)
return
}
r,err:=redis.Int(redisconnect.Do("get","tel"))
fmt.Println(r)}
//string多值
func acString2() {
_,err:=redisconnect.Do("mset","aaa",111,"bbb",222)
if err!=nil{
fmt.Println(err)
return
}
r,err:=redis.Ints(redisconnect.Do("mget","aaa","bbb"))
fmt.Println(r)
}
//列表操作list
func acList(){
_,err:=redisconnect.Do("lpush","booklist","aaaa","bbbb","cccc")
if err!=nil{fmt.Println(err)
return
}
r,err:=redis.String(redisconnect.Do("rpop","booklist"))
fmt.Println(r)
}
//hash
func achash(){
_,err:=redisconnect.Do("hmset","lissssst","A","111","B","222")
if err!=nil{
fmt.Println(err)
return
}
r,err:=redis.Strings(redisconnect.Do("hmget","lissssst","A"))
if err!=nil{
fmt.Println(err)
return
}
fmt.Println(r)
}