Skip to content

Commit 0e3ea5f

Browse files
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 <[email protected]>
1 parent f3a15c4 commit 0e3ea5f

File tree

1 file changed

+111
-3
lines changed

1 file changed

+111
-3
lines changed

doctests/cmds_hash_test.go

+111-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package example_commands_test
55
import (
66
"context"
77
"fmt"
8+
"sort"
89

910
"github.com/redis/go-redis/v9"
1011
)
@@ -74,8 +75,20 @@ func ExampleClient_hset() {
7475
panic(err)
7576
}
7677

77-
fmt.Println(res6)
78-
// >>> map[field1:Hello field2:Hi field3:World]
78+
keys := make([]string, 0, len(res6))
79+
80+
for key, _ := range res6 {
81+
keys = append(keys, key)
82+
}
83+
84+
sort.Strings(keys)
85+
86+
for _, key := range keys {
87+
fmt.Printf("Key: %v, value: %v\n", key, res6[key])
88+
}
89+
// >>> Key: field1, value: Hello
90+
// >>> Key: field2, value: Hi
91+
// >>> Key: field3, value: World
7992
// STEP_END
8093

8194
// Output:
@@ -84,7 +97,9 @@ func ExampleClient_hset() {
8497
// 2
8598
// Hi
8699
// World
87-
// map[field1:Hello field2:Hi field3:World]
100+
// Key: field1, value: Hello
101+
// Key: field2, value: Hi
102+
// Key: field3, value: World
88103
}
89104

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

0 commit comments

Comments
 (0)