当前位置:网站首页>go : go-redis list操作
go : go-redis list操作
2022-07-30 05:51:00 【行人已】
春江潮水连海平,海上明月共潮生!!!
具体代码在: 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 是把链接的单独剔除去当做一个方法引用,这里说一下坑:
// RedisDb 大写的字母表示公用方法,小写字母表示私有方法
//LPush : 此时列表不存在,依然可以插入
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:仅当Key存在的时候才插入数据,此时列表不存在,无法插入
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 (替换list中指定坐标的值)
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 (截取 /返回 ist中指定坐标的值 )
// 截取(可以理解为删除)名称为testList 的 key,并把截取后的值赋值给testList
val := config.RedisDb.LTrim("testList", 0, 3)
fmt.Println(val)
//返回 testList list中第三个值
index, err := config.RedisDb.LIndex("testList", 2).Result()
fmt.Println(index)
2.5 LPop & LRem (删除 值 )
//从列表左边删除第一个数据,并返回删除的数据 可以理解为删除最后一个数据
config.RedisDb.LPop("testList")
//删除列表中的数据。删除10个值为bee的数据重复元素
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 : 此时列表不存在,依然可以插入
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:仅当K存在的时候才插入数据,此时列表不存在,无法插入
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,并把截取后的值赋值给testList
val := config.RedisDb.LTrim("testList", 0, 3)
fmt.Println(val)
//返回 testList list中第三个值
index, err := config.RedisDb.LIndex("testList", 2).Result()
fmt.Println(index)
//LSet & LInsert
//替换 testList 中第三条数据
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示例
//从列表左边删除第一个数据,并返回删除的数据 可以理解为删除最后一个数据
config.RedisDb.LPop("testList")
//删除列表中的数据。删除10个值为bee的数据重复元素
config.RedisDb.LRem("testList", 10, "bee")
}
边栏推荐
- Selenium02
- (GGG)JWT
- AI can identify race from X-rays, but no one knows why
- The first artificial intelligence safety competition officially launched
- LVM和磁盘配额
- 深度学习:线性回归模型
- STL源码剖析:临时对象的代码测试和理解
- DNS domain name resolution services
- Go语学习笔记 - gorm使用 - 数据库配置、表新增 Web框架Gin(七)
- Pioneer in Distributed Systems - Leslie Lambert
猜你喜欢

New breakthrough in artificial muscle smart materials

阿里二面:列出 Api 接口优化的几个技巧

MYSQL-GROUP BY 用法 全网最精,通熟易懂的话解释

Test Development Engineer Growth Diary 001 - Some Introduction to Agile Testing, CI/CD/CT, DecOps

VR机器人教你如何正确打乒乓球

进程和计划任务管理

MYSQL下载及安装完整教程

【雷达目标检测】恒定阈值法和恒虚警(CFAR)法及代码实现

STL源码剖析:bound friend template friend代码测试和理解

MySQL主从复制配置搭建,一步到位
随机推荐
Test Development Engineer Growth Diary 010 - CI/CD/CT in Jenkins (Continuous Integration Build/Continuous Delivery/Continuous Testing)
STL源码剖析:class template explicit specialization代码测试和理解
千万级数据量的表,怎样最快速度查询?
阿里二面:列出 Api 接口优化的几个技巧
The first artificial intelligence safety competition officially launched
(GGG)JWT
阿里一面:多线程顺序运行有多少种方法?
Test development engineer diary 002 - starting from 0 interface automation
roslyn folder under bin folder
Proftpd配置文件
STL source code analysis: conceptual understanding of iterators, and code testing.
[硬核干货]由0到1,突破信息系统项目管理师(呕心沥血经验之谈)!!!
Derivative Operations on Vectors and Derivative Operations on Vector Cross and Dot Products
VR机器人教你如何正确打乒乓球
不会吧,Log4j 漏洞还没有完全修复?
Ali two sides: Sentinel vs Hystrix comparison, how to choose?
Electron中设置菜单(Menu),主进程向渲染进程共享数据
Linx常见目录&文件管理命令&VI编辑器使用 介绍
AI可通过X光片识别种族,但没人知道为什么
@Bean 与 @Component 用在同一个类上,会怎样?