MCPcopy
hub / github.com/redis/go-redis / ExampleClient_set_get_all

Function ExampleClient_set_get_all

doctests/hash_tutorial_test.go:14–95  ·  view source on GitHub ↗

HIDE_END

()

Source from the content-addressed store, hash-verified

12// HIDE_END
13
14func ExampleClient_set_get_all() {
15 ctx := context.Background()
16
17 rdb := redis.NewClient(&redis.Options{
18 Addr: "localhost:6379",
19 Password: "", // no password docs
20 DB: 0, // use default DB
21 })
22
23 // REMOVE_START
24 // make sure we are working with fresh database
25 rdb.FlushDB(ctx)
26 rdb.Del(ctx, "bike:1")
27 // REMOVE_END
28
29 // STEP_START set_get_all
30 hashFields := []string{
31 "model", "Deimos",
32 "brand", "Ergonom",
33 "type", "Enduro bikes",
34 "price", "4972",
35 }
36
37 res1, err := rdb.HSet(ctx, "bike:1", hashFields).Result()
38
39 if err != nil {
40 panic(err)
41 }
42
43 fmt.Println(res1) // >>> 4
44
45 res2, err := rdb.HGet(ctx, "bike:1", "model").Result()
46
47 if err != nil {
48 panic(err)
49 }
50
51 fmt.Println(res2) // >>> Deimos
52
53 res3, err := rdb.HGet(ctx, "bike:1", "price").Result()
54
55 if err != nil {
56 panic(err)
57 }
58
59 fmt.Println(res3) // >>> 4972
60
61 cmdReturn := rdb.HGetAll(ctx, "bike:1")
62 res4, err := cmdReturn.Result()
63
64 if err != nil {
65 panic(err)
66 }
67
68 fmt.Println(res4)
69 // >>> map[brand:Ergonom model:Deimos price:4972 type:Enduro bikes]
70
71 type BikeInfo struct {

Callers

nothing calls this directly

Calls 8

FlushDBMethod · 0.65
DelMethod · 0.65
ResultMethod · 0.65
HSetMethod · 0.65
HGetMethod · 0.65
HGetAllMethod · 0.65
ScanMethod · 0.65
PrintfMethod · 0.65

Tested by

no test coverage detected