...

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

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

     1  // EXAMPLE: cmds_string
     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_cmd_incr() {
    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, "mykey")
    27  	// REMOVE_END
    28  
    29  	// STEP_START incr
    30  	incrResult1, err := rdb.Set(ctx, "mykey", "10", 0).Result()
    31  
    32  	if err != nil {
    33  		panic(err)
    34  	}
    35  
    36  	fmt.Println(incrResult1) // >>> OK
    37  
    38  	incrResult2, err := rdb.Incr(ctx, "mykey").Result()
    39  
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  
    44  	fmt.Println(incrResult2) // >>> 11
    45  
    46  	incrResult3, err := rdb.Get(ctx, "mykey").Result()
    47  
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  
    52  	fmt.Println(incrResult3) // >>> 11
    53  	// STEP_END
    54  
    55  	// Output:
    56  	// OK
    57  	// 11
    58  	// 11
    59  }
    60  

View as plain text