| 152 | } |
| 153 | |
| 154 | func ExampleClient_hgetall() { |
| 155 | ctx := context.Background() |
| 156 | |
| 157 | rdb := redis.NewClient(&redis.Options{ |
| 158 | Addr: "localhost:6379", |
| 159 | Password: "", // no password |
| 160 | DB: 0, // use default DB |
| 161 | }) |
| 162 | |
| 163 | // REMOVE_START |
| 164 | // start with fresh database |
| 165 | rdb.FlushDB(ctx) |
| 166 | rdb.Del(ctx, "myhash") |
| 167 | // REMOVE_END |
| 168 | |
| 169 | // STEP_START hgetall |
| 170 | hGetAllResult1, err := rdb.HSet(ctx, "myhash", |
| 171 | "field1", "Hello", |
| 172 | "field2", "World", |
| 173 | ).Result() |
| 174 | |
| 175 | if err != nil { |
| 176 | panic(err) |
| 177 | } |
| 178 | |
| 179 | fmt.Println(hGetAllResult1) // >>> 2 |
| 180 | |
| 181 | hGetAllResult2, err := rdb.HGetAll(ctx, "myhash").Result() |
| 182 | |
| 183 | if err != nil { |
| 184 | panic(err) |
| 185 | } |
| 186 | |
| 187 | keys := make([]string, 0, len(hGetAllResult2)) |
| 188 | |
| 189 | for key := range hGetAllResult2 { |
| 190 | keys = append(keys, key) |
| 191 | } |
| 192 | |
| 193 | sort.Strings(keys) |
| 194 | |
| 195 | for _, key := range keys { |
| 196 | fmt.Printf("Key: %v, value: %v\n", key, hGetAllResult2[key]) |
| 197 | } |
| 198 | // >>> Key: field1, value: Hello |
| 199 | // >>> Key: field2, value: World |
| 200 | // STEP_END |
| 201 | |
| 202 | // Output: |
| 203 | // 2 |
| 204 | // Key: field1, value: Hello |
| 205 | // Key: field2, value: World |
| 206 | } |
| 207 | |
| 208 | func ExampleClient_hvals() { |
| 209 | ctx := context.Background() |