|
| 1 | +// EXAMPLE: cmds_hash |
| 2 | +// HIDE_START |
| 3 | +package example_commands_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/redis/go-redis/v9" |
| 10 | +) |
| 11 | + |
| 12 | +// HIDE_END |
| 13 | + |
| 14 | +func ExampleClient_hset() { |
| 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 | + rdb.Del(ctx, "myhash") |
| 25 | + // REMOVE_END |
| 26 | + |
| 27 | + // STEP_START hset |
| 28 | + res1, err := rdb.HSet(ctx, "myhash", "field1", "Hello").Result() |
| 29 | + |
| 30 | + if err != nil { |
| 31 | + panic(err) |
| 32 | + } |
| 33 | + |
| 34 | + fmt.Println(res1) // >>> 1 |
| 35 | + |
| 36 | + res2, err := rdb.HGet(ctx, "myhash", "field1").Result() |
| 37 | + |
| 38 | + if err != nil { |
| 39 | + panic(err) |
| 40 | + } |
| 41 | + |
| 42 | + fmt.Println(res2) // >>> Hello |
| 43 | + |
| 44 | + res3, err := rdb.HSet(ctx, "myhash", |
| 45 | + "field2", "Hi", |
| 46 | + "field3", "World", |
| 47 | + ).Result() |
| 48 | + |
| 49 | + if err != nil { |
| 50 | + panic(err) |
| 51 | + } |
| 52 | + |
| 53 | + fmt.Println(res3) // >>> 2 |
| 54 | + |
| 55 | + res4, err := rdb.HGet(ctx, "myhash", "field2").Result() |
| 56 | + |
| 57 | + if err != nil { |
| 58 | + panic(err) |
| 59 | + } |
| 60 | + |
| 61 | + fmt.Println(res4) // >>> Hi |
| 62 | + |
| 63 | + res5, err := rdb.HGet(ctx, "myhash", "field3").Result() |
| 64 | + |
| 65 | + if err != nil { |
| 66 | + panic(err) |
| 67 | + } |
| 68 | + |
| 69 | + fmt.Println(res5) // >>> World |
| 70 | + |
| 71 | + res6, err := rdb.HGetAll(ctx, "myhash").Result() |
| 72 | + |
| 73 | + if err != nil { |
| 74 | + panic(err) |
| 75 | + } |
| 76 | + |
| 77 | + fmt.Println(res6) |
| 78 | + // >>> map[field1:Hello field2:Hi field3:World] |
| 79 | + // STEP_END |
| 80 | + |
| 81 | + // Output: |
| 82 | + // 1 |
| 83 | + // Hello |
| 84 | + // 2 |
| 85 | + // Hi |
| 86 | + // World |
| 87 | + // map[field1:Hello field2:Hi field3:World] |
| 88 | +} |
| 89 | + |
| 90 | +func ExampleClient_hget() { |
| 91 | + ctx := context.Background() |
| 92 | + |
| 93 | + rdb := redis.NewClient(&redis.Options{ |
| 94 | + Addr: "localhost:6379", |
| 95 | + Password: "", // no password docs |
| 96 | + DB: 0, // use default DB |
| 97 | + }) |
| 98 | + |
| 99 | + // REMOVE_START |
| 100 | + rdb.Del(ctx, "myhash") |
| 101 | + // REMOVE_END |
| 102 | + |
| 103 | + // STEP_START hget |
| 104 | + res7, err := rdb.HSet(ctx, "myhash", "field1", "foo").Result() |
| 105 | + |
| 106 | + if err != nil { |
| 107 | + panic(err) |
| 108 | + } |
| 109 | + |
| 110 | + fmt.Println(res7) // >>> 1 |
| 111 | + |
| 112 | + res8, err := rdb.HGet(ctx, "myhash", "field1").Result() |
| 113 | + |
| 114 | + if err != nil { |
| 115 | + panic(err) |
| 116 | + } |
| 117 | + |
| 118 | + fmt.Println(res8) // >>> foo |
| 119 | + |
| 120 | + res9, err := rdb.HGet(ctx, "myhash", "field2").Result() |
| 121 | + |
| 122 | + if err != nil { |
| 123 | + fmt.Println(err) |
| 124 | + } |
| 125 | + |
| 126 | + fmt.Println(res9) // >>> <empty string> |
| 127 | + // STEP_END |
| 128 | + |
| 129 | + // Output: |
| 130 | + // 1 |
| 131 | + // foo |
| 132 | + // redis: nil |
| 133 | +} |
0 commit comments