Skip to content

DOC-4494 SADD and SMEMBERS command examples #3242

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

Merged
merged 16 commits into from
Mar 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions doctests/cmds_set_test.go
Original file line number Diff line number Diff line change
@@ -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]
}
Loading