当前位置:网站首页>go : go-redis list operation
go : go-redis list operation
2022-07-30 08:03:00 【pedestrians have】
春江潮水连海平,海上明月共潮生!!!
具体代码在: https://gitee.com/hjx_RuGuoYunZhiDao/strom-huang-go/blob/master/go_redis/go_redis_list.go
1、初始化redis
参考上一篇:https://blog.csdn.net/bei_FengBoby/article/details/124756296
2、list操作
2.1、LPushX & LPush (插入数据)
//config.RedisDb is to separate out the link as a method reference,Let's talk about the pit here:
// RedisDb Capital letters indicate public methods,Lowercase letters indicate private methods
//LPush : The list does not exist at this time,依然可以插入
n, err := config.RedisDb.LPush("testList", "tonoy1").Result()
fmt.Println("number:", n)
err = config.RedisDb.LPush("testList", "tonoy1", "tonoy3", "tonoy2").Err()
if err != nil {
//redis连接错误
panic(err)
}
//LPushX:仅当KeyInsert data when it exists,The list does not exist at this time,无法插入
t, err := config.RedisDb.LPushX("testList1", "aaa").Result()
fmt.Println("number:", t)
2.2、LRange & LLen (读取list指定坐标的值)
// 返回从0开始到-1位置之间的数据,意思就是返回全部数据
vals, err := config.RedisDb.LRange("testList", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(vals)
// 返回从0开始到2 位置之间的数据,意思就是返回全部数据
vals2, err := config.RedisDb.LRange("testList", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(vals2)
//返回list集合中的长度
testList, err := config.RedisDb.LLen("testList").Result()
if err != nil {
panic(err)
}
2.3、LSet & LInsert (替换listThe value of the specified coordinate in )
bee, err := config.RedisDb.LSet("testList", 2, "bee").Result()
fmt.Print(bee)
//在list列表testList 中值为bee 前面添加元素hello : 第二个参数有:after ,前面/后面
config.RedisDb.LInsert("testList", "before", "bee", "hello")
//config.RedisDb.LInsertBefore("studentList","lilei","hello") 执行效果一样
config.RedisDb.LInsert("testList", "after", "bee", "a")
//config.RedisDb.LInsertBefore("studentList","lilei","hello") 执行效果一样
2.4 LTrim & LIndex (截取 /返回 istThe value of the specified coordinate in )
// 截取(可以理解为删除)名称为testList 的 key,And assign the truncated value to testList
val := config.RedisDb.LTrim("testList", 0, 3)
fmt.Println(val)
//返回 testList listthe third value in
index, err := config.RedisDb.LIndex("testList", 2).Result()
fmt.Println(index)
2.5 LPop & LRem (删除 值 )
//从列表左边删除第一个数据,并返回删除的数据 It can be understood as deleting the last data
config.RedisDb.LPop("testList")
//删除列表中的数据.删除10个值为beedata repeating elements
config.RedisDb.LRem("testList", 10, "bee")
3、所有代码
package main
import (
"fmt"
config "strom-huang-go/go_redis/config"
)
func main() {
err := config.InitRedisClient()
if err != nil {
//redis连接错误
panic(err)
}
fmt.Println("Redis连接成功")
// -------------------------list-操作 -----------------------------------
//LPushX & LPush
//LPush : The list does not exist at this time,依然可以插入
n, err := config.RedisDb.LPush("testList", "tonoy1").Result()
fmt.Println("number:", n)
err = config.RedisDb.LPush("testList", "tonoy1", "tonoy3", "tonoy2").Err()
if err != nil {
//redis连接错误
panic(err)
}
//LPushX:仅当KInsert data when it exists,The list does not exist at this time,无法插入
t, err := config.RedisDb.LPushX("testList1", "aaa").Result()
fmt.Println("number:", t)
// LRange & LLen
// 返回从0开始到-1位置之间的数据,意思就是返回全部数据
vals, err := config.RedisDb.LRange("testList", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(vals)
// 返回从0开始到2 位置之间的数据,意思就是返回全部数据
vals2, err := config.RedisDb.LRange("testList", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(vals2)
//返回list集合中的长度
testList, err := config.RedisDb.LLen("testList").Result()
if err != nil {
panic(err)
}
fmt.Println("testList集合的长度为:", testList)
// LTrim & LIndex
// 截取(可以理解为删除)名称为testList 的 key,And assign the truncated value to testList
val := config.RedisDb.LTrim("testList", 0, 3)
fmt.Println(val)
//返回 testList listthe third value in
index, err := config.RedisDb.LIndex("testList", 2).Result()
fmt.Println(index)
//LSet & LInsert
//替换 testList the third data
bee, err := config.RedisDb.LSet("testList", 2, "bee").Result()
fmt.Print(bee)
//在list列表testList 中值为bee 前面添加元素hello : 第二个参数有:after ,前面/后面
config.RedisDb.LInsert("testList", "before", "bee", "hello")
//config.RedisDb.LInsertBefore("studentList","lilei","hello") 执行效果一样
config.RedisDb.LInsert("testList", "after", "bee", "a")
//config.RedisDb.LInsertBefore("studentList","lilei","hello") 执行效果一样
//LPop & LRem示例
//从列表左边删除第一个数据,并返回删除的数据 It can be understood as deleting the last data
config.RedisDb.LPop("testList")
//删除列表中的数据.删除10个值为beedata repeating elements
config.RedisDb.LRem("testList", 10, "bee")
}
边栏推荐
猜你喜欢

专访蚂蚁:这群技术排头兵,如何做好底层开发这件事?| 卓越技术团队访谈录

The introduction of AI meta-learning into neuroscience, the medical effect is expected to improve accurately

预测人们对你的第一印象,“AI颜狗”的诞生

云服务器零基础部署网站(保姆级教程)

The CTO said I was not advised to use SELECT *, why is that?

Go 结合Gin导出Mysql数据到Excel表格

相机坐标系,世界坐标系,像素坐标系三者转换,以及OPENGLDEFocal Length和Opengl 的 Fov转换

《心智社会》—马文·明斯基

Redis 如何实现防止超卖和库存扣减操作?

STL源码剖析:迭代器的概念理解,以及代码测试。
随机推荐
Develop common tool software
Keil编译大小和存储说明
Go 结合Gin导出Mysql数据到Excel表格
go : go-redis 基础操作
Polygon 3D(三维平面多边形)的法向量的计算(MeshLab默认的计算)
ArrayList
golang: Gorm配置Mysql多数据源
从追赶到超越,国产软件大显身手
头条二面:MySQL中有几种常见的 SQL 错误用法?
go : 使用gorm修改数据
go : go-redis set操作
MySQL什么时候用表锁,什么时候用行锁?
What new materials are used in the large aircraft C919?
MYSQL下载及安装完整教程
MySQL基础篇【命名规范】
Headline 2: there are several kinds of common SQL errors in MySQL usage?
首届人工智能安全大赛正式启动
Pioneer in Distributed Systems - Leslie Lambert
idea built-in translation plugin
window.open()的用法,js打开新窗体