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