| 95 | } |
| 96 | |
| 97 | func ExampleClient_hmget() { |
| 98 | ctx := context.Background() |
| 99 | |
| 100 | rdb := redis.NewClient(&redis.Options{ |
| 101 | Addr: "localhost:6379", |
| 102 | Password: "", // no password docs |
| 103 | DB: 0, // use default DB |
| 104 | }) |
| 105 | |
| 106 | // REMOVE_START |
| 107 | // start with fresh database |
| 108 | rdb.FlushDB(ctx) |
| 109 | rdb.Del(ctx, "bike:1") |
| 110 | // REMOVE_END |
| 111 | |
| 112 | hashFields := []string{ |
| 113 | "model", "Deimos", |
| 114 | "brand", "Ergonom", |
| 115 | "type", "Enduro bikes", |
| 116 | "price", "4972", |
| 117 | } |
| 118 | |
| 119 | _, err := rdb.HSet(ctx, "bike:1", hashFields).Result() |
| 120 | |
| 121 | if err != nil { |
| 122 | panic(err) |
| 123 | } |
| 124 | |
| 125 | // STEP_START hmget |
| 126 | cmdReturn := rdb.HMGet(ctx, "bike:1", "model", "price") |
| 127 | res5, err := cmdReturn.Result() |
| 128 | |
| 129 | if err != nil { |
| 130 | panic(err) |
| 131 | } |
| 132 | |
| 133 | fmt.Println(res5) // >>> [Deimos 4972] |
| 134 | |
| 135 | type BikeInfo struct { |
| 136 | Model string `redis:"model"` |
| 137 | Brand string `redis:"-"` |
| 138 | Type string `redis:"-"` |
| 139 | Price int `redis:"price"` |
| 140 | } |
| 141 | |
| 142 | var res5a BikeInfo |
| 143 | |
| 144 | if err := cmdReturn.Scan(&res5a); err != nil { |
| 145 | panic(err) |
| 146 | } |
| 147 | |
| 148 | fmt.Printf("Model: %v, Price: $%v\n", res5a.Model, res5a.Price) |
| 149 | // >>> Model: Deimos, Price: $4972 |
| 150 | // STEP_END |
| 151 | |
| 152 | // Output: |
| 153 | // [Deimos 4972] |
| 154 | // Model: Deimos, Price: $4972 |