当前位置:网站首页>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")
}
边栏推荐
- go : go-redis set operations
- Let the "label" content in Baidu map generator expand--solution
- go : go-redis list操作
- go : go-redis 基础操作
- What are the access modifiers, declaration modifiers, and keywords in C#?Literacy articles
- Go uses the mencached cache
- 深度学习:线性回归模型
- go : 使用 grom 删除数据库数据
- idea built-in translation plugin
- Go 结合Gin导出Mysql数据到Excel表格
猜你喜欢

RAID disk array

Playing script killing with AI: actually more involved than me

C#的访问修饰符,声明修饰符,关键字有哪些?扫盲篇

DNS domain name resolution services

The Society of Mind - Marvin Minsky

【MySQL】MySQL中如何实现分页操作

Linx common directory & file management commands & VI editor usage introduction

不会吧,Log4j 漏洞还没有完全修复?
![[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及](/img/ac/80ab67505f7df52d92a206bc3dd50e.png)
[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及

How to understand plucker coordinates (geometric understanding)
随机推荐
The calculation proof of the intersection of the space line and the plane and its source code
Oracle查看表空间使用率及爆满解决方案
Universal js time date format conversion
What new materials are used in the large aircraft C919?
No, the Log4j vulnerability hasn't been fully fixed yet?
schur completement
@Bean 与 @Component 用在同一个类上,会怎样?
深度学习:线性回归模型
架构设计指南 如何成为架构师
便携小风扇PD取电芯片
New breakthrough in artificial muscle smart materials
How does Redis prevent oversold and inventory deduction operations?
[硬核干货]由0到1,突破信息系统项目管理师(呕心沥血经验之谈)!!!
UDP和TCP使用同一个端口,可行吗?
Go 使用mencached缓存
debian problem
sql concat()函数
redis实现分布式锁的原理
Playing script killing with AI: actually more involved than me
上传文件--文件类型大全,图片类型,文档类型,视频类型,压缩包类型