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

Function main

example/scan-struct/main.go:23–83  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

21}
22
23func main() {
24 ctx := context.Background()
25
26 rdb := redis.NewClient(&redis.Options{
27 Addr: ":6379",
28 })
29 _ = rdb.FlushDB(ctx).Err()
30
31 // Set some fields.
32 if _, err := rdb.Pipelined(ctx, func(rdb redis.Pipeliner) error {
33 rdb.HSet(ctx, "key", "str1", "hello")
34 rdb.HSet(ctx, "key", "str2", "world")
35 rdb.HSet(ctx, "key", "str3", "")
36 rdb.HSet(ctx, "key", "int", 123)
37 rdb.HSet(ctx, "key", "int2", 0)
38 rdb.HSet(ctx, "key", "bool", 1)
39 rdb.HSet(ctx, "key", "bool2", 0)
40 rdb.HSet(ctx, "key", "bytes", []byte("this is bytes !"))
41 return nil
42 }); err != nil {
43 panic(err)
44 }
45
46 var model1, model2 Model
47
48 // Scan all fields into the model.
49 if err := rdb.HGetAll(ctx, "key").Scan(&model1); err != nil {
50 panic(err)
51 }
52
53 // Or scan a subset of the fields.
54 if err := rdb.HMGet(ctx, "key", "str1", "int").Scan(&model2); err != nil {
55 panic(err)
56 }
57
58 spew.Dump(model1)
59 // Output:
60 // (main.Model) {
61 // Str1: (string) (len=5) "hello",
62 // Str2: (string) (len=5) "world",
63 // Bytes: ([]uint8) (len=15 cap=16) {
64 // 00000000 74 68 69 73 20 69 73 20 62 79 74 65 73 20 21 |this is bytes !|
65 // },
66 // Int: (int) 123,
67 // Bool: (bool) true,
68 // Ignored: (struct {}) {
69 // }
70 // }
71
72 spew.Dump(model2)
73 // Output:
74 // (main.Model) {
75 // Str1: (string) (len=5) "hello",
76 // Str2: (string) "",
77 // Bytes: ([]uint8) <nil>,
78 // Int: (int) 123,
79 // Bool: (bool) false,
80 // Ignored: (struct {}) {

Callers

nothing calls this directly

Calls 8

PipelinedMethod · 0.95
ErrMethod · 0.65
FlushDBMethod · 0.65
HSetMethod · 0.65
ScanMethod · 0.65
HGetAllMethod · 0.65
HMGetMethod · 0.65
DumpMethod · 0.65

Tested by

no test coverage detected