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

Function main

example/hset-struct/main.go:31–125  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

29}
30
31func main() {
32 ctx := context.Background()
33
34 rdb := redis.NewClient(&redis.Options{
35 Addr: ":6379",
36 })
37
38 _ = rdb.FlushDB(ctx).Err()
39
40 t := time.Date(2025, 02, 8, 0, 0, 0, 0, time.UTC)
41
42 data := Model{
43 Str1: "hello",
44 Str2: "world",
45 Str3: ToPtr("hello"),
46 Str4: nil,
47 Bytes: []byte("this is bytes !"),
48 Int: 123,
49 Int2: ToPtr(0),
50 Int3: nil,
51 Bool: true,
52 Bool2: ToPtr(false),
53 Bool3: nil,
54 Time: t,
55 Time2: ToPtr(t),
56 Time3: nil,
57 Ignored: struct{}{},
58 }
59
60 // Set some fields.
61 if _, err := rdb.Pipelined(ctx, func(rdb redis.Pipeliner) error {
62 rdb.HMSet(ctx, "key", data)
63 return nil
64 }); err != nil {
65 panic(err)
66 }
67
68 var model1, model2 Model
69
70 // Scan all fields into the model.
71 if err := rdb.HGetAll(ctx, "key").Scan(&model1); err != nil {
72 panic(err)
73 }
74
75 // Or scan a subset of the fields.
76 if err := rdb.HMGet(ctx, "key", "str1", "int").Scan(&model2); err != nil {
77 panic(err)
78 }
79
80 spew.Dump(model1)
81 // Output:
82 // (main.Model) {
83 // Str1: (string) (len=5) "hello",
84 // Str2: (string) (len=5) "world",
85 // Str3: (*string)(0xc000016970)((len=5) "hello"),
86 // Str4: (*string)(0xc000016980)(""),
87 // Bytes: ([]uint8) (len=15 cap=16) {
88 // 00000000 74 68 69 73 20 69 73 20 62 79 74 65 73 20 21 |this is bytes !|

Callers

nothing calls this directly

Calls 9

PipelinedMethod · 0.95
ToPtrFunction · 0.70
ErrMethod · 0.65
FlushDBMethod · 0.65
HMSetMethod · 0.65
ScanMethod · 0.65
HGetAllMethod · 0.65
HMGetMethod · 0.65
DumpMethod · 0.65

Tested by

no test coverage detected