HIDE_END
()
| 13 | // HIDE_END |
| 14 | |
| 15 | func ExampleClient_hset() { |
| 16 | ctx := context.Background() |
| 17 | |
| 18 | rdb := redis.NewClient(&redis.Options{ |
| 19 | Addr: "localhost:6379", |
| 20 | Password: "", // no password docs |
| 21 | DB: 0, // use default DB |
| 22 | }) |
| 23 | |
| 24 | // REMOVE_START |
| 25 | // make sure we are working with fresh database |
| 26 | rdb.FlushDB(ctx) |
| 27 | rdb.Del(ctx, "myhash") |
| 28 | // REMOVE_END |
| 29 | |
| 30 | // STEP_START hset |
| 31 | res1, err := rdb.HSet(ctx, "myhash", "field1", "Hello").Result() |
| 32 | |
| 33 | if err != nil { |
| 34 | panic(err) |
| 35 | } |
| 36 | |
| 37 | fmt.Println(res1) // >>> 1 |
| 38 | |
| 39 | res2, err := rdb.HGet(ctx, "myhash", "field1").Result() |
| 40 | |
| 41 | if err != nil { |
| 42 | panic(err) |
| 43 | } |
| 44 | |
| 45 | fmt.Println(res2) // >>> Hello |
| 46 | |
| 47 | res3, err := rdb.HSet(ctx, "myhash", |
| 48 | "field2", "Hi", |
| 49 | "field3", "World", |
| 50 | ).Result() |
| 51 | |
| 52 | if err != nil { |
| 53 | panic(err) |
| 54 | } |
| 55 | |
| 56 | fmt.Println(res3) // >>> 2 |
| 57 | |
| 58 | res4, err := rdb.HGet(ctx, "myhash", "field2").Result() |
| 59 | |
| 60 | if err != nil { |
| 61 | panic(err) |
| 62 | } |
| 63 | |
| 64 | fmt.Println(res4) // >>> Hi |
| 65 | |
| 66 | res5, err := rdb.HGet(ctx, "myhash", "field3").Result() |
| 67 | |
| 68 | if err != nil { |
| 69 | panic(err) |
| 70 | } |
| 71 | |
| 72 | fmt.Println(res5) // >>> World |