...

Source file src/github.com/redis/go-redis/v9/doctests/bitmap_tutorial_test.go

Documentation: github.com/redis/go-redis/v9/doctests

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

View as plain text