ExampleMapStringStringCmd_Scan shows how to scan the results of a map fetch into a struct.
()
| 347 | // ExampleMapStringStringCmd_Scan shows how to scan the results of a map fetch |
| 348 | // into a struct. |
| 349 | func 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. |