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

Function ExampleMapStringStringCmd_Scan

example_test.go:349–379  ·  view source on GitHub ↗

ExampleMapStringStringCmd_Scan shows how to scan the results of a map fetch into a struct.

()

Source from the content-addressed store, hash-verified

347// ExampleMapStringStringCmd_Scan shows how to scan the results of a map fetch
348// into a struct.
349func ExampleMapStringStringCmd_Scan() {
350 rdb.FlushDB(ctx)
351 err := rdb.HMSet(ctx, "map",
352 "name", "hello",
353 "count", 123,
354 "correct", true).Err()
355 if err != nil {
356 panic(err)
357 }
358
359 // Get the map. The same approach works for HmGet().
360 res := rdb.HGetAll(ctx, "map")
361 if res.Err() != nil {
362 panic(res.Err())
363 }
364
365 type data struct {
366 Name string `redis:"name"`
367 Count int `redis:"count"`
368 Correct bool `redis:"correct"`
369 }
370
371 // Scan the results into the struct.
372 var d data
373 if err := res.Scan(&d); err != nil {
374 panic(err)
375 }
376
377 fmt.Println(d)
378 // Output: {hello 123 true}
379}
380
381// ExampleSliceCmd_Scan shows how to scan the results of a multi key fetch
382// into a struct.

Callers

nothing calls this directly

Calls 5

FlushDBMethod · 0.65
ErrMethod · 0.65
HMSetMethod · 0.65
HGetAllMethod · 0.65
ScanMethod · 0.65

Tested by

no test coverage detected