mirror of
https://github.com/redis/go-redis.git
synced 2025-04-16 09:23:06 +03:00
DOC-4449 hash command examples (#3229)
* DOC-4450 added hgetall and hvals doc examples * DOC-4449 added hgetall and hvals doc examples * DOC-4449 rewrote to avoid Collect and Keys functions (not available in test version of Go) * DOC-4449 replaced slices.Sort function with older alternative * DOC-4449 removed another instance of slices.Sort * DOC-4449 fixed bugs in tests * DOC-4449 try sort.Strings() for sorting key lists --------- Co-authored-by: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com>
This commit is contained in:
parent
f3a15c401e
commit
0e3ea5fd6b
@ -5,6 +5,7 @@ package example_commands_test
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
@ -74,8 +75,20 @@ func ExampleClient_hset() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println(res6)
|
||||
// >>> map[field1:Hello field2:Hi field3:World]
|
||||
keys := make([]string, 0, len(res6))
|
||||
|
||||
for key, _ := range res6 {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, key := range keys {
|
||||
fmt.Printf("Key: %v, value: %v\n", key, res6[key])
|
||||
}
|
||||
// >>> Key: field1, value: Hello
|
||||
// >>> Key: field2, value: Hi
|
||||
// >>> Key: field3, value: World
|
||||
// STEP_END
|
||||
|
||||
// Output:
|
||||
@ -84,7 +97,9 @@ func ExampleClient_hset() {
|
||||
// 2
|
||||
// Hi
|
||||
// World
|
||||
// map[field1:Hello field2:Hi field3:World]
|
||||
// Key: field1, value: Hello
|
||||
// Key: field2, value: Hi
|
||||
// Key: field3, value: World
|
||||
}
|
||||
|
||||
func ExampleClient_hget() {
|
||||
@ -131,3 +146,96 @@ func ExampleClient_hget() {
|
||||
// foo
|
||||
// redis: nil
|
||||
}
|
||||
|
||||
func ExampleClient_hgetall() {
|
||||
ctx := context.Background()
|
||||
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
Password: "", // no password
|
||||
DB: 0, // use default DB
|
||||
})
|
||||
|
||||
// REMOVE_START
|
||||
rdb.Del(ctx, "myhash")
|
||||
// REMOVE_END
|
||||
|
||||
// STEP_START hgetall
|
||||
hGetAllResult1, err := rdb.HSet(ctx, "myhash",
|
||||
"field1", "Hello",
|
||||
"field2", "World",
|
||||
).Result()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println(hGetAllResult1) // >>> 2
|
||||
|
||||
hGetAllResult2, err := rdb.HGetAll(ctx, "myhash").Result()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
keys := make([]string, 0, len(hGetAllResult2))
|
||||
|
||||
for key, _ := range hGetAllResult2 {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, key := range keys {
|
||||
fmt.Printf("Key: %v, value: %v\n", key, hGetAllResult2[key])
|
||||
}
|
||||
// >>> Key: field1, value: Hello
|
||||
// >>> Key: field2, value: World
|
||||
// STEP_END
|
||||
|
||||
// Output:
|
||||
// 2
|
||||
// Key: field1, value: Hello
|
||||
// Key: field2, value: World
|
||||
}
|
||||
|
||||
func ExampleClient_hvals() {
|
||||
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, "myhash")
|
||||
// REMOVE_END
|
||||
|
||||
// STEP_START hvals
|
||||
hValsResult1, err := rdb.HSet(ctx, "myhash",
|
||||
"field1", "Hello",
|
||||
"field2", "World",
|
||||
).Result()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println(hValsResult1) // >>> 2
|
||||
|
||||
hValsResult2, err := rdb.HVals(ctx, "myhash").Result()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
sort.Strings(hValsResult2)
|
||||
|
||||
fmt.Println(hValsResult2) // >>> [Hello World]
|
||||
// STEP_END
|
||||
|
||||
// Output:
|
||||
// 2
|
||||
// [Hello World]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user