...

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

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

     1  // EXAMPLE: set_and_get
     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  func ExampleClient_Set_and_get() {
    13  	ctx := context.Background()
    14  
    15  	rdb := redis.NewClient(&redis.Options{
    16  		Addr:     "localhost:6379",
    17  		Password: "", // no password docs
    18  		DB:       0,  // use default DB
    19  	})
    20  
    21  	// HIDE_END
    22  
    23  	// REMOVE_START
    24  	// start with fresh database
    25  	rdb.FlushDB(ctx)
    26  	errFlush := rdb.FlushDB(ctx).Err() // Clear the database before each test
    27  	if errFlush != nil {
    28  		panic(errFlush)
    29  	}
    30  	// REMOVE_END
    31  
    32  	err := rdb.Set(ctx, "bike:1", "Process 134", 0).Err()
    33  	if err != nil {
    34  		panic(err)
    35  	}
    36  
    37  	fmt.Println("OK")
    38  
    39  	value, err := rdb.Get(ctx, "bike:1").Result()
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  	fmt.Printf("The name of the bike is %s", value)
    44  	// HIDE_START
    45  
    46  	// Output: OK
    47  	// The name of the bike is Process 134
    48  }
    49  
    50  // HIDE_END
    51  

View as plain text