Redis List Operations

Learn how list operations can be invoked using the go-redis client.

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
...