Skip to content

Commit 3e8a3d9

Browse files
DOC-4450 added hgetall and hvals doc examples
1 parent 91dddc2 commit 3e8a3d9

File tree

1 file changed

+104
-3
lines changed

1 file changed

+104
-3
lines changed

doctests/cmds_hash_test.go

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package example_commands_test
55
import (
66
"context"
77
"fmt"
8+
"maps"
9+
"slices"
810

911
"github.com/redis/go-redis/v9"
1012
)
@@ -74,8 +76,16 @@ func ExampleClient_hset() {
7476
panic(err)
7577
}
7678

77-
fmt.Println(res6)
78-
// >>> map[field1:Hello field2:Hi field3:World]
79+
keys := slices.Collect(maps.Keys(res6))
80+
81+
slices.Sort(keys)
82+
83+
for key, value := range res6 {
84+
fmt.Printf("Key: %v, value: %v\n", key, value)
85+
}
86+
// >>> Key: field1, value: Hello
87+
// >>> Key: field2, value: Hi
88+
// >>> Key: field3, value: World
7989
// STEP_END
8090

8191
// Output:
@@ -84,7 +94,9 @@ func ExampleClient_hset() {
8494
// 2
8595
// Hi
8696
// World
87-
// map[field1:Hello field2:Hi field3:World]
97+
// Key: field1, value: Hello
98+
// Key: field2, value: Hi
99+
// Key: field3, value: World
88100
}
89101

90102
func ExampleClient_hget() {
@@ -131,3 +143,92 @@ func ExampleClient_hget() {
131143
// foo
132144
// redis: nil
133145
}
146+
147+
func ExampleClient_hgetall() {
148+
ctx := context.Background()
149+
150+
rdb := redis.NewClient(&redis.Options{
151+
Addr: "localhost:6379",
152+
Password: "", // no password docs
153+
DB: 0, // use default DB
154+
})
155+
156+
// REMOVE_START
157+
rdb.Del(ctx, "myhash")
158+
// REMOVE_END
159+
160+
// STEP_START hgetall
161+
hGetAllResult1, err := rdb.HSet(ctx, "myhash",
162+
"field1", "Hello",
163+
"field2", "World",
164+
).Result()
165+
166+
if err != nil {
167+
panic(err)
168+
}
169+
170+
fmt.Println(hGetAllResult1) // >>> 2
171+
172+
hGetAllResult2, err := rdb.HGetAll(ctx, "myhash").Result()
173+
174+
if err != nil {
175+
panic(err)
176+
}
177+
178+
keys := slices.Collect(maps.Keys(hGetAllResult2))
179+
180+
slices.Sort(keys)
181+
182+
for key, value := range hGetAllResult2 {
183+
fmt.Printf("Key: %v, value: %v\n", key, value)
184+
}
185+
// >>> Key: field1, value: Hello
186+
// >>> Key: field2, value: World
187+
// STEP_END
188+
189+
// Output:
190+
// 2
191+
// Key: field1, value: Hello
192+
// Key: field2, value: World
193+
}
194+
195+
func ExampleClient_hvals() {
196+
ctx := context.Background()
197+
198+
rdb := redis.NewClient(&redis.Options{
199+
Addr: "localhost:6379",
200+
Password: "", // no password docs
201+
DB: 0, // use default DB
202+
})
203+
204+
// REMOVE_START
205+
rdb.Del(ctx, "myhash")
206+
// REMOVE_END
207+
208+
// STEP_START hvals
209+
hValsResult1, err := rdb.HSet(ctx, "myhash",
210+
"field1", "Hello",
211+
"field2", "World",
212+
).Result()
213+
214+
if err != nil {
215+
panic(err)
216+
}
217+
218+
fmt.Println(hValsResult1) // >>> 2
219+
220+
hValsResult2, err := rdb.HVals(ctx, "myhash").Result()
221+
222+
if err != nil {
223+
panic(err)
224+
}
225+
226+
slices.Sort(hValsResult2)
227+
228+
fmt.Println(hValsResult2) // >>> [Hello World]
229+
// STEP_END
230+
231+
// Output:
232+
// 2
233+
// [Hello World]
234+
}

0 commit comments

Comments
 (0)