mirror of
https://github.com/redis/go-redis.git
synced 2025-04-17 20:17:02 +03:00
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 <nedyalko.dyakov@gmail.com>
This commit is contained in:
parent
a402c55344
commit
74d4f08476
102
doctests/cmds_set_test.go
Normal file
102
doctests/cmds_set_test.go
Normal file
@ -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…
x
Reference in New Issue
Block a user