|
| 1 | +// EXAMPLE: bitmap_tutorial |
| 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_ping() { |
| 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, "pings:2024-01-01-00:00") |
| 25 | +// REMOVE_END |
| 26 | + |
| 27 | +// STEP_START ping |
| 28 | +res1, err := rdb.SetBit(ctx, "pings:2024-01-01-00:00", 123, 1).Result() |
| 29 | + |
| 30 | +if err != nil { |
| 31 | +panic(err) |
| 32 | +} |
| 33 | + |
| 34 | +fmt.Println(res1) // >>> 0 |
| 35 | + |
| 36 | +res2, err := rdb.GetBit(ctx, "pings:2024-01-01-00:00", 123).Result() |
| 37 | + |
| 38 | +if err != nil { |
| 39 | +panic(err) |
| 40 | +} |
| 41 | + |
| 42 | +fmt.Println(res2) // >>> 1 |
| 43 | + |
| 44 | +res3, err := rdb.GetBit(ctx, "pings:2024-01-01-00:00", 456).Result() |
| 45 | + |
| 46 | +if err != nil { |
| 47 | +panic(err) |
| 48 | +} |
| 49 | + |
| 50 | +fmt.Println(res3) // >>> 0 |
| 51 | +// STEP_END |
| 52 | + |
| 53 | +// Output: |
| 54 | +// 0 |
| 55 | +// 1 |
| 56 | +// 0 |
| 57 | +} |
| 58 | + |
| 59 | +func ExampleClient_bitcount() { |
| 60 | +ctx := context.Background() |
| 61 | + |
| 62 | +rdb := redis.NewClient(&redis.Options{ |
| 63 | +Addr: "localhost:6379", |
| 64 | +Password: "", // no password docs |
| 65 | +DB: 0, // use default DB |
| 66 | +}) |
| 67 | + |
| 68 | +// REMOVE_START |
| 69 | +_, err := rdb.SetBit(ctx, "pings:2024-01-01-00:00", 123, 1).Result() |
| 70 | + |
| 71 | +if err != nil { |
| 72 | +panic(err) |
| 73 | +} |
| 74 | +// REMOVE_END |
| 75 | + |
| 76 | +// STEP_START bitcount |
| 77 | +res4, err := rdb.BitCount(ctx, "pings:2024-01-01-00:00", |
| 78 | +&redis.BitCount{ |
| 79 | +Start: 0, |
| 80 | +End: 456, |
| 81 | +}).Result() |
| 82 | + |
| 83 | +if err != nil { |
| 84 | +panic(err) |
| 85 | +} |
| 86 | + |
| 87 | +fmt.Println(res4) // >>> 1 |
| 88 | +// STEP_END |
| 89 | + |
| 90 | +// Output: |
| 91 | +// 1 |
| 92 | +} |
0 commit comments