5

【笔记】Go语言操作Redis

 1 year ago
source link: https://loli.fj.cn/2023/05/20/Go%E8%AF%AD%E8%A8%80%E6%93%8D%E4%BD%9CRedis/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【笔记】Go语言操作Redis

2023-05-202023-05-26Go语言学习指北

Go语言操作Redis学习笔记

go get github.com/gomodule/redigo/redis

连接数据库

conn, _ := redis.Dial("tcp", ":6379")
defer conn.Close()
conn.Do("set", "key", "value")
  • 先通过send()方法将命令添加到缓冲区,再通过flush()方法执行命令
conn.Send("set", "key", "value")
conn.Flush()
  • 先通过Send()方法将命令添加到缓冲区,再通过Flush()方法执行命令,最后通过Receive()方法获取返回值
conn.Send("get", "key")
conn.Flush()
res, err := conn.Receive()

将查询到的结果强制类型转换

转换为string类型

res, err := redis.String(conn.Do("get", "key"))

转换为bool类型

res, err := redis.Bool(conn.Do("get", "key"))

转换为int类型

res, err := redis.Int(conn.Do("get", "key"))

取出字节流

  • 取出多种数据类型的数据
  • 先去除字节流数据,再扫描数据的数据类型
res, err := redis.Values(conn.Do("mget", "key1", "key2"))
var key1 string
var key2 int
redis.Scan(res, &key1, &key2)

哔哩哔哩——地鼠文档


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK