1.三方库安装
309_尚硅谷_Go连接到Redis_哔哩哔哩_bilibili
借鉴:
Golang 安装 Redis_go fiber 安装redis_柒柒伍贰玖。的博客-CSDN博客
三方redis库已经迁移到以下网址,go get github.com/gomodule/redigo/redis
gomodule/redigo: Go client for Redis (github.com)
注意下载的文件是在GOPATH/src下面,而不是在GOPATH下。
2.连接redis,Get/Set操作String
1)连接
必须提前控制台运行redis
package mainimport ("fmt""github.com/gomodule/redigo/redis" //"github.com/garyburd/redigo/redis" //弃用
)func main() {//通过go向redis写入和读取数据//1.链接redis(协议,发送ip:端口)conn, err := redis.Dial("tcp", "localhost:6379")if err != nil {fmt.Println("redis.Dial err=", err)return}fmt.Println("连接成功", conn)defer conn.Close()
}
输出套接字
tips:redis.Dial err= dial tcp [::1]:6379: connectex: No connection could be made because the target machine actively refused it.
多半是因为没有提前打开redis。
想要链接redis,必须提前运行redis
Could not connect to redis connection refused - Fix it easily (bobcares.com)
2)Set
//2.通过go向redis写入数据 string [key,val]_, err = conn.Do("Set", "name", "tom猫")if err != nil {fmt.Println("Set err=", err)return}fmt.Println("set成功")
3)Get
package mainimport ("fmt""github.com/gomodule/redigo/redis" //"github.com/garyburd/redigo/redis" //弃用
)func main() {//通过go向redis写入和读取数据//1.链接redis(协议,发送ip:端口)conn, err := redis.Dial("tcp", "localhost:6379")if err != nil {fmt.Println("redis.Dial err=", err)return}fmt.Println("连接成功", conn)defer conn.Close()//2.通过go向redis写入数据 string [key,val]_, err = conn.Do("Set", "name", "tom猫")if err != nil {fmt.Println("Set err=", err)return}fmt.Println("set成功")//3.通过go向redis读取数据 string [key,val]r, err := redis.String(conn.Do("Get", "name"))if err != nil {fmt.Println("Get err=", err)return}//返回r是interface{}类型,需要断言string//nameString := r.(string)//这样不好,用内置方法redis.Stringfmt.Println("get成功 r =", r)
}
3.操作hash
基本就是把上面的string操作, 换一个指令
1)单个hget/hset
package mainimport ("fmt""github.com/gomodule/redigo/redis" //"github.com/garyburd/redigo/redis" //弃用
)func main() {//通过go向redis写入和读取数据//1.链接redis(协议,发送ip:端口)conn, err := redis.Dial("tcp", "localhost:6379")if err != nil {fmt.Println("redis.Dial err=", err)return}fmt.Println("连接成功", conn)defer conn.Close()//2.通过go向redis写入数据 string [key,val]_, err = conn.Do("HSet", "user01", "name", "john")if err != nil {fmt.Println("hSet err=", err)return}_, err = conn.Do("HSet", "user01", "age", 18)if err != nil {fmt.Println("hSet err=", err)return}fmt.Println("hset成功")//3.通过go向redis读取数据 string [key,val]r1, err := redis.String(conn.Do("HGet", "user01", "name"))if err != nil {fmt.Println("hGet err=", err)return}r2, err := redis.String(conn.Do("HGet", "user01", "age"))if err != nil {fmt.Println("hGet err=", err)return}//返回r是interface{}类型,需要断言string//nameString := r.(string)//这样不好,用内置方法redis.Stringfmt.Println("hget成功 r1 =", r1, " r2 =", r2)
}
2)批量hmget/hmset
package mainimport ("fmt""github.com/gomodule/redigo/redis" //"github.com/garyburd/redigo/redis" //弃用
)/*批量hash*/
func main() {//通过go向redis写入和读取数据//1.链接redis(协议,发送ip:端口)conn, err := redis.Dial("tcp", "localhost:6379")if err != nil {fmt.Println("redis.Dial err=", err)return}fmt.Println("连接成功", conn)defer conn.Close()//2.通过go向redis写入数据 string [key,val]_, err = conn.Do("HMSet", "user02", "name", "john", "age", 19)if err != nil {fmt.Println("hSet err=", err)return}fmt.Println("hset成功")//3.通过go向redis读取数据 string [key,val]//取一个用户的多个属性r, err := redis.Strings(conn.Do("HMGet", "user02", "name", "age"))if err != nil {fmt.Println("hGet err=", err)return}fmt.Println(r) // 切片for i, k := range r {fmt.Printf("r[%v]=%v \n", i, k)}}
3)给数据设置有效时间