Files
go/doctests/cmds_list_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

324 lines
5.3 KiB
Go

// EXAMPLE: cmds_list
// HIDE_START
package example_commands_test
import (
"context"
"fmt"
"github.com/hanzokv/go/v9"
)
// HIDE_END
func ExampleClient_cmd_llen() {
ctx := context.Background()
rdb := kv.NewClient(&kv.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
rdb.Del(ctx, "mylist")
// REMOVE_END
// STEP_START llen
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
lLenResult, err := rdb.LLen(ctx, "mylist").Result()
if err != nil {
panic(err)
}
fmt.Println(lLenResult) // >>> 2
// STEP_END
// Output:
// 1
// 2
// 2
}
func ExampleClient_cmd_lpop() {
ctx := context.Background()
rdb := kv.NewClient(&kv.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
rdb.Del(ctx, "mylist")
// REMOVE_END
// STEP_START lpop
RPushResult, err := rdb.RPush(ctx,
"mylist", "one", "two", "three", "four", "five",
).Result()
if err != nil {
panic(err)
}
fmt.Println(RPushResult) // >>> 5
lPopResult, err := rdb.LPop(ctx, "mylist").Result()
if err != nil {
panic(err)
}
fmt.Println(lPopResult) // >>> one
lPopCountResult, err := rdb.LPopCount(ctx, "mylist", 2).Result()
if err != nil {
panic(err)
}
fmt.Println(lPopCountResult) // >>> [two three]
lRangeResult, err := rdb.LRange(ctx, "mylist", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult) // >>> [four five]
// STEP_END
// Output:
// 5
// one
// [two three]
// [four five]
}
func ExampleClient_cmd_lpush() {
ctx := context.Background()
rdb := kv.NewClient(&kv.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
lRangeResult, err := rdb.LRange(ctx, "mylist", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult) // >>> [Hello World]
// STEP_END
// Output:
// 1
// 2
// [Hello World]
}
func ExampleClient_cmd_lrange() {
ctx := context.Background()
rdb := kv.NewClient(&kv.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
rdb.Del(ctx, "mylist")
// REMOVE_END
// STEP_START lrange
RPushResult, err := rdb.RPush(ctx, "mylist",
"one", "two", "three",
).Result()
if err != nil {
panic(err)
}
fmt.Println(RPushResult) // >>> 3
lRangeResult1, err := rdb.LRange(ctx, "mylist", 0, 0).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult1) // >>> [one]
lRangeResult2, err := rdb.LRange(ctx, "mylist", -3, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult2) // >>> [one two three]
lRangeResult3, err := rdb.LRange(ctx, "mylist", -100, 100).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult3) // >>> [one two three]
lRangeResult4, err := rdb.LRange(ctx, "mylist", 5, 10).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult4) // >>> []
// STEP_END
// Output:
// 3
// [one]
// [one two three]
// [one two three]
// []
}
func ExampleClient_cmd_rpop() {
ctx := context.Background()
rdb := kv.NewClient(&kv.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
rdb.Del(ctx, "mylist")
// REMOVE_END
// STEP_START rpop
rPushResult, err := rdb.RPush(ctx, "mylist",
"one", "two", "three", "four", "five",
).Result()
if err != nil {
panic(err)
}
fmt.Println(rPushResult) // >>> 5
rPopResult, err := rdb.RPop(ctx, "mylist").Result()
if err != nil {
panic(err)
}
fmt.Println(rPopResult) // >>> five
rPopCountResult, err := rdb.RPopCount(ctx, "mylist", 2).Result()
if err != nil {
panic(err)
}
fmt.Println(rPopCountResult) // >>> [four three]
lRangeResult, err := rdb.LRange(ctx, "mylist", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult) // >>> [one two]
// STEP_END
// Output:
// 5
// five
// [four three]
// [one two]
}
func ExampleClient_cmd_rpush() {
ctx := context.Background()
rdb := kv.NewClient(&kv.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
lRangeResult, err := rdb.LRange(ctx, "mylist", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult) // >>> [Hello World]
// STEP_END
// Output:
// 1
// 2
// [Hello World]
}