HIDE_END
()
| 12 | // HIDE_END |
| 13 | |
| 14 | func ExampleClient_set_get_all() { |
| 15 | ctx := context.Background() |
| 16 | |
| 17 | rdb := redis.NewClient(&redis.Options{ |
| 18 | Addr: "localhost:6379", |
| 19 | Password: "", // no password docs |
| 20 | DB: 0, // use default DB |
| 21 | }) |
| 22 | |
| 23 | // REMOVE_START |
| 24 | // make sure we are working with fresh database |
| 25 | rdb.FlushDB(ctx) |
| 26 | rdb.Del(ctx, "bike:1") |
| 27 | // REMOVE_END |
| 28 | |
| 29 | // STEP_START set_get_all |
| 30 | hashFields := []string{ |
| 31 | "model", "Deimos", |
| 32 | "brand", "Ergonom", |
| 33 | "type", "Enduro bikes", |
| 34 | "price", "4972", |
| 35 | } |
| 36 | |
| 37 | res1, err := rdb.HSet(ctx, "bike:1", hashFields).Result() |
| 38 | |
| 39 | if err != nil { |
| 40 | panic(err) |
| 41 | } |
| 42 | |
| 43 | fmt.Println(res1) // >>> 4 |
| 44 | |
| 45 | res2, err := rdb.HGet(ctx, "bike:1", "model").Result() |
| 46 | |
| 47 | if err != nil { |
| 48 | panic(err) |
| 49 | } |
| 50 | |
| 51 | fmt.Println(res2) // >>> Deimos |
| 52 | |
| 53 | res3, err := rdb.HGet(ctx, "bike:1", "price").Result() |
| 54 | |
| 55 | if err != nil { |
| 56 | panic(err) |
| 57 | } |
| 58 | |
| 59 | fmt.Println(res3) // >>> 4972 |
| 60 | |
| 61 | cmdReturn := rdb.HGetAll(ctx, "bike:1") |
| 62 | res4, err := cmdReturn.Result() |
| 63 | |
| 64 | if err != nil { |
| 65 | panic(err) |
| 66 | } |
| 67 | |
| 68 | fmt.Println(res4) |
| 69 | // >>> map[brand:Ergonom model:Deimos price:4972 type:Enduro bikes] |
| 70 | |
| 71 | type BikeInfo struct { |