From e113512c184cfaebc6560782706bd02d93884cf7 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Tue, 2 Feb 2021 16:28:35 +0530 Subject: [PATCH] Add example showing scanning to struct --- example_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/example_test.go b/example_test.go index 161eabf6..07eb5108 100644 --- a/example_test.go +++ b/example_test.go @@ -276,6 +276,40 @@ func ExampleClient_ScanType() { // Output: found 33 keys } +// ExampleStringStringMapCmd_Scan shows how to scan the results of a map fetch +// into a struct. +func ExampleStringStringMapCmd_Scan() { + rdb.FlushDB(ctx) + err := rdb.HMSet(ctx, "map", + "name", "hello", + "count", 123, + "correct", true).Err() + if err != nil { + panic(err) + } + + // Get the map. The same approach works for HmGet(). + res := rdb.HGetAll(ctx, "map") + if res.Err() != nil { + panic(err) + } + + type data struct { + Name string `redis:"name"` + Count int `redis:"count"` + Correct bool `redis:"correct"` + } + + // Scan the results into the struct. + var d data + if err := res.Scan(&d); err != nil { + panic(err) + } + + fmt.Println(d) + // Output: {hello 123 true} +} + func ExampleClient_Pipelined() { var incr *redis.IntCmd _, err := rdb.Pipelined(ctx, func(pipe redis.Pipeliner) error {