Files
go/doctests/sets_example_test.go
zeekay 6994cf05aa kv, not redis: rename the package, the extras and the URL scheme
We do not use Redis. The client was a go-redis fork and still said so
everywhere: `package redis`, `redis.NewClient`, `redis://`, `extra/redisotel`.
Consumers wrote `redis.Client` against a Valkey-backed store called KV.

  package redis            -> package kv          (93 decls)
  redis.X                  -> kv.X                (2,794 call sites)
  "redis: …" errors        -> "kv: …"             (206)
  extra/redis{otel,cmd,prometheus,census} -> extra/kv{otel,cmd,prometheus,census}
  redis:// , rediss://     -> kv:// , kvs://      (ParseURL)
  Redis*/‌*Redis* idents    -> KV*/*KV*
  REDIS_VERSION, REDIS_ENDPOINTS_CONFIG_PATH -> KV_*

I previously argued this rename was unsafe because much of it was "wire
contract". That was wrong, and the categorisation shows why: RESP command names
are GET/SET/HSET — they never contain the word. Of 5,771 occurrences only two
kinds were server-visible at all: the ParseURL scheme (we choose what we pass;
our configs already use kv://) and the CLIENT SETINFO lib-name label, which the
server merely records. Nothing here changes a byte of protocol.

Two things deliberately keep the old spelling:
  RediSearch  — names a specific external module the FT.* commands map to.
  StoreDist   — contains the letters by accident (sto-reDis-t), not the word.

Breaking for consumers by design: every import site must move redis.X -> kv.X.
Minor bump, never a major — this module stays v9 forever.
2026-07-26 08:34:19 -07:00

474 lines
8.9 KiB
Go

// EXAMPLE: sets_tutorial
// HIDE_START
package example_commands_test
import (
"context"
"fmt"
"sort"
"github.com/hanzokv/go/v9"
)
// HIDE_END
func ExampleClient_sadd() {
ctx := context.Background()
rdb := kv.NewClient(&kv.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
// start with fresh database
rdb.FlushDB(ctx)
rdb.Del(ctx, "bikes:racing:france")
rdb.Del(ctx, "bikes:racing:usa")
// REMOVE_END
// STEP_START sadd
res1, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 1
res2, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> 0
res3, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> 2
res4, err := rdb.SAdd(ctx, "bikes:racing:usa", "bike:1", "bike:4").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> 2
// STEP_END
// Output:
// 1
// 0
// 2
// 2
}
func ExampleClient_sismember() {
ctx := context.Background()
rdb := kv.NewClient(&kv.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
// start with fresh database
rdb.FlushDB(ctx)
rdb.Del(ctx, "bikes:racing:france")
rdb.Del(ctx, "bikes:racing:usa")
// REMOVE_END
_, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
_, err = rdb.SAdd(ctx, "bikes:racing:usa", "bike:1", "bike:4").Result()
if err != nil {
panic(err)
}
// STEP_START sismember
res5, err := rdb.SIsMember(ctx, "bikes:racing:usa", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res5) // >>> true
res6, err := rdb.SIsMember(ctx, "bikes:racing:usa", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res6) // >>> false
// STEP_END
// Output:
// true
// false
}
func ExampleClient_sinter() {
ctx := context.Background()
rdb := kv.NewClient(&kv.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
// start with fresh database
rdb.FlushDB(ctx)
rdb.Del(ctx, "bikes:racing:france")
rdb.Del(ctx, "bikes:racing:usa")
// REMOVE_END
_, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
_, err = rdb.SAdd(ctx, "bikes:racing:usa", "bike:1", "bike:4").Result()
if err != nil {
panic(err)
}
// STEP_START sinter
res7, err := rdb.SInter(ctx, "bikes:racing:france", "bikes:racing:usa").Result()
if err != nil {
panic(err)
}
fmt.Println(res7) // >>> [bike:1]
// STEP_END
// Output:
// [bike:1]
}
func ExampleClient_scard() {
ctx := context.Background()
rdb := kv.NewClient(&kv.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
// start with fresh database
rdb.FlushDB(ctx)
rdb.Del(ctx, "bikes:racing:france")
// REMOVE_END
_, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
// STEP_START scard
res8, err := rdb.SCard(ctx, "bikes:racing:france").Result()
if err != nil {
panic(err)
}
fmt.Println(res8) // >>> 3
// STEP_END
// Output:
// 3
}
func ExampleClient_saddsmembers() {
ctx := context.Background()
rdb := kv.NewClient(&kv.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
// start with fresh database
rdb.FlushDB(ctx)
rdb.Del(ctx, "bikes:racing:france")
// REMOVE_END
// STEP_START sadd_smembers
res9, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res9) // >>> 3
res10, err := rdb.SMembers(ctx, "bikes:racing:france").Result()
if err != nil {
panic(err)
}
// Sort the strings in the slice to make sure the output is lexicographical
sort.Strings(res10)
fmt.Println(res10) // >>> [bike:1 bike:2 bike:3]
// STEP_END
// Output:
// 3
// [bike:1 bike:2 bike:3]
}
func ExampleClient_smismember() {
ctx := context.Background()
rdb := kv.NewClient(&kv.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
// start with fresh database
rdb.FlushDB(ctx)
rdb.Del(ctx, "bikes:racing:france")
// REMOVE_END
_, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
// STEP_START smismember
res11, err := rdb.SIsMember(ctx, "bikes:racing:france", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res11) // >>> true
res12, err := rdb.SMIsMember(ctx, "bikes:racing:france", "bike:2", "bike:3", "bike:4").Result()
if err != nil {
panic(err)
}
fmt.Println(res12) // >>> [true true false]
// STEP_END
// Output:
// true
// [true true false]
}
func ExampleClient_sdiff() {
ctx := context.Background()
rdb := kv.NewClient(&kv.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
// start with fresh database
rdb.FlushDB(ctx)
rdb.Del(ctx, "bikes:racing:france")
rdb.Del(ctx, "bikes:racing:usa")
// REMOVE_END
// STEP_START sdiff
_, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
_, err = rdb.SAdd(ctx, "bikes:racing:usa", "bike:1", "bike:4").Result()
res13, err := rdb.SDiff(ctx, "bikes:racing:france", "bikes:racing:usa").Result()
if err != nil {
panic(err)
}
// Sort the strings in the slice to make sure the output is lexicographical
sort.Strings(res13)
fmt.Println(res13) // >>> [bike:2 bike:3]
// STEP_END
// Output:
// [bike:2 bike:3]
}
func ExampleClient_multisets() {
ctx := context.Background()
rdb := kv.NewClient(&kv.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
// start with fresh database
rdb.FlushDB(ctx)
rdb.Del(ctx, "bikes:racing:france")
rdb.Del(ctx, "bikes:racing:usa")
rdb.Del(ctx, "bikes:racing:italy")
// REMOVE_END
// STEP_START multisets
_, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
_, err = rdb.SAdd(ctx, "bikes:racing:usa", "bike:1", "bike:4").Result()
if err != nil {
panic(err)
}
_, err = rdb.SAdd(ctx, "bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4").Result()
if err != nil {
panic(err)
}
res14, err := rdb.SInter(ctx, "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy").Result()
if err != nil {
panic(err)
}
fmt.Println(res14) // >>> [bike:1]
res15, err := rdb.SUnion(ctx, "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy").Result()
if err != nil {
panic(err)
}
// Sort the strings in the slice to make sure the output is lexicographical
sort.Strings(res15)
fmt.Println(res15) // >>> [bike:1 bike:2 bike:3 bike:4]
res16, err := rdb.SDiff(ctx, "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy").Result()
if err != nil {
panic(err)
}
fmt.Println(res16) // >>> []
res17, err := rdb.SDiff(ctx, "bikes:racing:usa", "bikes:racing:france").Result()
if err != nil {
panic(err)
}
fmt.Println(res17) // >>> [bike:4]
res18, err := rdb.SDiff(ctx, "bikes:racing:france", "bikes:racing:usa").Result()
if err != nil {
panic(err)
}
// Sort the strings in the slice to make sure the output is lexicographical
sort.Strings(res18)
fmt.Println(res18) // >>> [bike:2 bike:3]
// STEP_END
// Output:
// [bike:1]
// [bike:1 bike:2 bike:3 bike:4]
// []
// [bike:4]
// [bike:2 bike:3]
}
func ExampleClient_srem() {
ctx := context.Background()
rdb := kv.NewClient(&kv.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
// start with fresh database
rdb.FlushDB(ctx)
rdb.Del(ctx, "bikes:racing:france")
// REMOVE_END
// STEP_START srem
_, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
res19, err := rdb.SRem(ctx, "bikes:racing:france", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res19) // >>> 1
res20, err := rdb.SPop(ctx, "bikes:racing:france").Result()
if err != nil {
panic(err)
}
fmt.Println(res20) // >>> <random element>
res21, err := rdb.SMembers(ctx, "bikes:racing:france").Result()
if err != nil {
panic(err)
}
fmt.Println(res21) // >>> <remaining elements>
res22, err := rdb.SRandMember(ctx, "bikes:racing:france").Result()
if err != nil {
panic(err)
}
fmt.Println(res22) // >>> <random element>
// STEP_END
// Testable examples not available because the test output
// is not deterministic.
}