-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DOC-4464 examples for llen, lpop, lpush, lrange, rpop, and rpush #3234
Open
andy-stark-redis
wants to merge
9
commits into
redis:master
Choose a base branch
from
andy-stark-redis:DOC-4464-list-cmd-examples
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3c3a598
DOC-4464 examples for llen, lpop, lpush, lrange, rpop, and rpush
andy-stark-redis be6cf9a
Merge branch 'master' into DOC-4464-list-cmd-examples
andy-stark-redis a2b9410
Merge branch 'master' into DOC-4464-list-cmd-examples
andy-stark-redis 6ef8e3c
Merge branch 'master' into DOC-4464-list-cmd-examples
andy-stark-redis 0df1e49
Merge branch 'master' into DOC-4464-list-cmd-examples
andy-stark-redis 568b98f
Merge branch 'master' into DOC-4464-list-cmd-examples
vladvildanov cca94f7
Merge branch 'master' into DOC-4464-list-cmd-examples
ndyakov ac1a0dd
Merge branch 'master' into DOC-4464-list-cmd-examples
andy-stark-redis d08366e
Merge branch 'master' into DOC-4464-list-cmd-examples
ndyakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,323 @@ | ||
// EXAMPLE: cmds_list | ||
// HIDE_START | ||
package example_commands_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
// HIDE_END | ||
|
||
func ExampleClient_cmd_llen() { | ||
ctx := context.Background() | ||
|
||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: "localhost:6379", | ||
Password: "", // no password docs | ||
DB: 0, // use default DB | ||
}) | ||
|
||
// REMOVE_START | ||
rdb.Del(ctx, "mylist") | ||
// REMOVE_END | ||
|
||
// STEP_START llen | ||
lLenResult1, err := rdb.LPush(ctx, "mylist", "World").Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lLenResult1) // >>> 1 | ||
|
||
lLenResult2, err := rdb.LPush(ctx, "mylist", "Hello").Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lLenResult2) // >>> 2 | ||
|
||
lLenResult3, err := rdb.LLen(ctx, "mylist").Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lLenResult3) // >>> 2 | ||
// STEP_END | ||
|
||
// Output: | ||
// 1 | ||
// 2 | ||
// 2 | ||
} | ||
func ExampleClient_cmd_lpop() { | ||
ctx := context.Background() | ||
|
||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: "localhost:6379", | ||
Password: "", // no password docs | ||
DB: 0, // use default DB | ||
}) | ||
|
||
// REMOVE_START | ||
rdb.Del(ctx, "mylist") | ||
// REMOVE_END | ||
|
||
// STEP_START lpop | ||
lPopResult1, err := rdb.RPush(ctx, | ||
"mylist", "one", "two", "three", "four", "five", | ||
).Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lPopResult1) // >>> 5 | ||
|
||
lPopResult2, err := rdb.LPop(ctx, "mylist").Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lPopResult2) // >>> one | ||
|
||
lPopResult3, err := rdb.LPopCount(ctx, "mylist", 2).Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lPopResult3) // >>> [two three] | ||
|
||
lPopResult4, err := rdb.LRange(ctx, "mylist", 0, -1).Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lPopResult4) // >>> [four five] | ||
// STEP_END | ||
|
||
// Output: | ||
// 5 | ||
// one | ||
// [two three] | ||
// [four five] | ||
} | ||
|
||
func ExampleClient_cmd_lpush() { | ||
ctx := context.Background() | ||
|
||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: "localhost:6379", | ||
Password: "", // no password docs | ||
DB: 0, // use default DB | ||
}) | ||
|
||
// REMOVE_START | ||
rdb.Del(ctx, "mylist") | ||
// REMOVE_END | ||
|
||
// STEP_START lpush | ||
lPushResult1, err := rdb.LPush(ctx, "mylist", "World").Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lPushResult1) // >>> 1 | ||
|
||
lPushResult2, err := rdb.LPush(ctx, "mylist", "Hello").Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lPushResult2) // >>> 2 | ||
|
||
lPushResult3, err := rdb.LRange(ctx, "mylist", 0, -1).Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lPushResult3) // >>> [Hello World] | ||
// STEP_END | ||
|
||
// Output: | ||
// 1 | ||
// 2 | ||
// [Hello World] | ||
} | ||
|
||
func ExampleClient_cmd_lrange() { | ||
ctx := context.Background() | ||
|
||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: "localhost:6379", | ||
Password: "", // no password docs | ||
DB: 0, // use default DB | ||
}) | ||
|
||
// REMOVE_START | ||
rdb.Del(ctx, "mylist") | ||
// REMOVE_END | ||
|
||
// STEP_START lrange | ||
lRangeResult1, err := rdb.RPush(ctx, "mylist", | ||
"one", "two", "three", | ||
).Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lRangeResult1) // >>> 3 | ||
|
||
lRangeResult2, err := rdb.LRange(ctx, "mylist", 0, 0).Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lRangeResult2) // >>> [one] | ||
|
||
lRangeResult3, err := rdb.LRange(ctx, "mylist", -3, 2).Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lRangeResult3) // >>> [one two three] | ||
|
||
lRangeResult4, err := rdb.LRange(ctx, "mylist", -100, 100).Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lRangeResult4) // >>> [one two three] | ||
|
||
lRangeResult5, err := rdb.LRange(ctx, "mylist", 5, 10).Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(lRangeResult5) // >>> [] | ||
// STEP_END | ||
|
||
// Output: | ||
// 3 | ||
// [one] | ||
// [one two three] | ||
// [one two three] | ||
// [] | ||
} | ||
|
||
func ExampleClient_cmd_rpop() { | ||
ctx := context.Background() | ||
|
||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: "localhost:6379", | ||
Password: "", // no password docs | ||
DB: 0, // use default DB | ||
}) | ||
|
||
// REMOVE_START | ||
rdb.Del(ctx, "mylist") | ||
// REMOVE_END | ||
|
||
// STEP_START rpop | ||
rPopResult1, err := rdb.RPush(ctx, "mylist", | ||
"one", "two", "three", "four", "five", | ||
).Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(rPopResult1) // >>> 5 | ||
|
||
rPopResult2, err := rdb.RPop(ctx, "mylist").Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(rPopResult2) // >>> five | ||
|
||
rPopResult3, err := rdb.RPopCount(ctx, "mylist", 2).Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(rPopResult3) // >>> [four three] | ||
|
||
rPopResult4, err := rdb.LRange(ctx, "mylist", 0, -1).Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(rPopResult4) // >>> [one two] | ||
// STEP_END | ||
|
||
// Output: | ||
// 5 | ||
// five | ||
// [four three] | ||
// [one two] | ||
} | ||
|
||
func ExampleClient_cmd_rpush() { | ||
ctx := context.Background() | ||
|
||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: "localhost:6379", | ||
Password: "", // no password docs | ||
DB: 0, // use default DB | ||
}) | ||
|
||
// REMOVE_START | ||
rdb.Del(ctx, "mylist") | ||
// REMOVE_END | ||
|
||
// STEP_START rpush | ||
rPushResult1, err := rdb.RPush(ctx, "mylist", "Hello").Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(rPushResult1) // >>> 1 | ||
|
||
rPushResult2, err := rdb.RPush(ctx, "mylist", "World").Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(rPushResult2) // >>> 2 | ||
|
||
rPushResult3, err := rdb.LRange(ctx, "mylist", 0, -1).Result() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(rPushResult3) // >>> [Hello World] | ||
// STEP_END | ||
|
||
// Output: | ||
// 1 | ||
// 2 | ||
// [Hello World] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change and use better variable names? For example, when the result is from
LPush
, if you would like to follow the same naming you do in other examples, makes sense the variable to be namedlPushResult
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ndyakov As with the set commands, the result variable name comes from the name of the command that the example is for (in this case, all the code in the example is for the
LLEN
page, so the vars arelLenResultxx
). With theLPUSH
example, it's just a coincidence that the first command in the code is anlpush(...)
, which is why the var name relates to the command there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see... Maybe we can use other naming convention? At least for me it was misleading having
lLenResult2
as a result ofLPush
.