| 134 | } |
| 135 | |
| 136 | func ExampleClient_incr() { |
| 137 | ctx := context.Background() |
| 138 | |
| 139 | rdb := redis.NewClient(&redis.Options{ |
| 140 | Addr: "localhost:6379", |
| 141 | Password: "", // no password docs |
| 142 | DB: 0, // use default DB |
| 143 | }) |
| 144 | |
| 145 | // REMOVE_START |
| 146 | // start with fresh database |
| 147 | rdb.FlushDB(ctx) |
| 148 | rdb.Del(ctx, "total_crashes") |
| 149 | // REMOVE_END |
| 150 | |
| 151 | // STEP_START incr |
| 152 | res8, err := rdb.Set(ctx, "total_crashes", "0", 0).Result() |
| 153 | |
| 154 | if err != nil { |
| 155 | panic(err) |
| 156 | } |
| 157 | |
| 158 | fmt.Println(res8) // >>> OK |
| 159 | |
| 160 | res9, err := rdb.Incr(ctx, "total_crashes").Result() |
| 161 | |
| 162 | if err != nil { |
| 163 | panic(err) |
| 164 | } |
| 165 | |
| 166 | fmt.Println(res9) // >>> 1 |
| 167 | |
| 168 | res10, err := rdb.IncrBy(ctx, "total_crashes", 10).Result() |
| 169 | |
| 170 | if err != nil { |
| 171 | panic(err) |
| 172 | } |
| 173 | |
| 174 | fmt.Println(res10) // >>> 11 |
| 175 | // STEP_END |
| 176 | |
| 177 | // Output: |
| 178 | // OK |
| 179 | // 1 |
| 180 | // 11 |
| 181 | } |