Search⌘ K
AI Features

Redis List Operations

Understand and apply Redis list commands such as LPush, RPush, LLen, LRange, LPop, and LTrim to manage lists in Go. Learn about blocking operations BRPop and BLPop for real-time messaging applications. This lesson equips you with essential skills to use Redis lists effectively in building scalable messaging systems.

The LPush method

To add items to a list, we can use the LPush method; for example, the code below will insert five elements to my-list. With this method, items are inserted at the head of the list.

client.LPush(context.Background(), "my-list", "item-1", "item-2", "item-3", "item-4", "item-5")
Add items to a list: LPush

The RPush method

It’s also possible to add items to a list using the RPush method. With this method, items are inserted at the tail of the list.

client.RPush(context.Background(), "my-list", "item-1", "item-2", "item-3", "item-4", "item-5")
Add items to a list: RPush

The LLen method

We can use the LLen method to get the number of items in list. In this case, my-list has five elements.

numOfItems := client.LLen(context.Background(), "my-list").Val()
Find the length of a list
...