golang redis之list型別簡單操作

2021-08-11 07:31:44 字數 3621 閱讀 7875

1:安裝redigo

go get github.com/garyburd/redigo/redis
2:連線redis(通過ip埠)

3:命令執行

n,err := c.do("lpush","key","value")  //寫

result,err := redis.values(c.do("lpop","key"))//讀

4:list型別常見操作

rpush(key, value):在名稱為key的list尾新增乙個值為value的元素

lpush(key, value):在名稱為key的list頭新增乙個值為value的 元素

llen(key):返回名稱為key的list的長度

lrange(key, start, end):返回名稱為key的list中start至end之間的元素

ltrim(key, start, end):擷取名稱為key的list

lindex(key, index):返回名稱為key的list中index位置的元素

lset(key, index, value):給名稱為key的list中index位置的元素賦值

lrem(key, count, value):刪除count個key的list中值為value的元素

lpop(key):返回並刪除名稱為key的list中的首元素

rpop(key):返回並刪除名稱為key的list中的尾元素

blpop(key1, key2,… key n, timeout):lpop命令的block版本。

brpop(key1, key2,… key n, timeout):rpop的block版本。

rpoplpush(srckey, dstkey):返回並刪除名稱為srckey的list的尾元素,並將該元素新增到名稱為dstkey的list的頭部

5:示例

5.1:lpush示例

_,err = c.do("lpush","mylist","ofo","mobike","foo")

iferr != nil

5.2:rpush示例

_,err = c.do("rpush","mylist","bluegogo","xiaolan","xiaoming")

iferr != nil

5.3:llen示例

num,err := c.do("llen","mylist")

iferr != nilelse

5.4:lrange示例

values,err := redis.values(c.do("lrange","mylist",0,10))

if err != nil

fmt.printf("mylist is:")

for _,v := range values

fmt.println()

5.5:ltrim示例

_, err = c.do("ltrim", "mylist", 0, 4)

iferr != nil else

fmt.printf("ltrim mylist is:")

for _, v := range values

fmt.printf("\n")

}

5.6:lindex示例

val, err := c.do("lindex", "mylist", 2)

iferr != nil else

5.7:rpoplpush示例

_, err = c.do("rpoplpush", "mylist", "mybike")

if err != nil else

for _, v := range values

values, err = redis.values(c.do("lrange", "mybike", 0, 10))

if err != nil

for _, v := range values

}

5.8:lset示例

_, err = c.do("lset", "mylist", 2, "mysql")

iferr != nil

val, err = c.do("lindex", "mylist", 2)

iferr != nil else

5.9:lrem示例

_, err = c.do("lrem", "mylist", 1, "mysql")

iferr != nil else

for _, v := range values

fmt.printf("\n")

}

5.10:lpop示例

_, err = c.do("lpop", "mylist")

iferr != nil else

fmt.printf("lpop mylist :")

for _, v := range values

fmt.printf("\n")

}

5.11:rpop示例

_, err = c.do("rpop", "mylist")

if err != nil else

fmt.printf("rpop mylist :")

for _, v := range values

fmt.printf("\n")

}

5.12:blpop示例

res, err := c.do("blpop", "mylist", 10)

iferr != nil else

res, err = c.do("blpop", "tmpbike", 10)

iferr != nil else

5.13:brpop示例

res, err = c.do("brpop", "tmpbike", 10)

iferr != nil else

res, err = c.do("brpop", "mybike", 10)

iferr != nil else

6:結果

完整**路徑

golang redis之set型別簡單操作

1 安裝redigo go get github.com garyburd redigo redis2 連線redis 3 命令執行 n,err c.do sadd key value 寫 result,err redis.values c.do smembers key 讀 4 set型別常見操作...

Golang Redis分布式鎖

分布式鎖實現 獲取分布式鎖 func h handler acquirelock cuuid string error return nil return errors.new set distribute lock failed 釋放分布式鎖 func h handler releaselock ...

function object研究之list1分析

首先看一下bind.hpp中的list0模板定義 cpp class list0 template t operator bi value v const template t const operator bi value const v const template t operator ref...