...

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

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

     1  // EXAMPLE: bitfield_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_bf() {
    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, "bike:1:stats")
    27  	// REMOVE_END
    28  
    29  	// STEP_START bf
    30  	res1, err := rdb.BitField(ctx, "bike:1:stats",
    31  		"set", "u32", "#0", "1000",
    32  	).Result()
    33  
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  
    38  	fmt.Println(res1) // >>> [0]
    39  
    40  	res2, err := rdb.BitField(ctx,
    41  		"bike:1:stats",
    42  		"incrby", "u32", "#0", "-50",
    43  		"incrby", "u32", "#1", "1",
    44  	).Result()
    45  
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  
    50  	fmt.Println(res2) // >>> [950 1]
    51  
    52  	res3, err := rdb.BitField(ctx,
    53  		"bike:1:stats",
    54  		"incrby", "u32", "#0", "500",
    55  		"incrby", "u32", "#1", "1",
    56  	).Result()
    57  
    58  	if err != nil {
    59  		panic(err)
    60  	}
    61  
    62  	fmt.Println(res3) // >>> [1450 2]
    63  
    64  	res4, err := rdb.BitField(ctx, "bike:1:stats",
    65  		"get", "u32", "#0",
    66  		"get", "u32", "#1",
    67  	).Result()
    68  
    69  	if err != nil {
    70  		panic(err)
    71  	}
    72  
    73  	fmt.Println(res4) // >>> [1450 2]
    74  	// STEP_END
    75  
    76  	// Output:
    77  	// [0]
    78  	// [950 1]
    79  	// [1450 2]
    80  	// [1450 2]
    81  }
    82  

View as plain text