From 74d4f084764d855c811ebd2c57193f74fe0ec7ad Mon Sep 17 00:00:00 2001 From: andy-stark-redis <164213578+andy-stark-redis@users.noreply.github.com> Date: Wed, 19 Mar 2025 15:36:58 +0000 Subject: [PATCH] DOC-4494 SADD and SMEMBERS command examples (#3242) * DOC-4494 added sadd example * DOC-4494 sadd and smembers examples * DOC-4494 better naming convention for result variables --------- Co-authored-by: Nedyalko Dyakov --- doctests/cmds_set_test.go | 102 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 doctests/cmds_set_test.go diff --git a/doctests/cmds_set_test.go b/doctests/cmds_set_test.go new file mode 100644 index 00000000..fecddbb8 --- /dev/null +++ b/doctests/cmds_set_test.go @@ -0,0 +1,102 @@ +// EXAMPLE: cmds_set +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_sadd_cmd() { + 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, "myset") + // REMOVE_END + + // STEP_START sadd + sAddResult1, err := rdb.SAdd(ctx, "myset", "Hello").Result() + + if err != nil { + panic(err) + } + + fmt.Println(sAddResult1) // >>> 1 + + sAddResult2, err := rdb.SAdd(ctx, "myset", "World").Result() + + if err != nil { + panic(err) + } + + fmt.Println(sAddResult2) // >>> 1 + + sAddResult3, err := rdb.SAdd(ctx, "myset", "World").Result() + + if err != nil { + panic(err) + } + + fmt.Println(sAddResult3) // >>> 0 + + sMembersResult, err := rdb.SMembers(ctx, "myset").Result() + + if err != nil { + panic(err) + } + + fmt.Println(sMembersResult) // >>> [Hello World] + // STEP_END + + // Output: + // 1 + // 1 + // 0 + // [Hello World] +} + +func ExampleClient_smembers_cmd() { + 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, "myset") + // REMOVE_END + + // STEP_START smembers + sAddResult, err := rdb.SAdd(ctx, "myset", "Hello", "World").Result() + + if err != nil { + panic(err) + } + + fmt.Println(sAddResult) // >>> 2 + + sMembersResult, err := rdb.SMembers(ctx, "myset").Result() + + if err != nil { + panic(err) + } + + fmt.Println(sMembersResult) // >>> [Hello World] + // STEP_END + + // Output: + // 2 + // [Hello World] +}