...

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

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

     1  // EXAMPLE: lpush_and_lrange
     2  // HIDE_START
     3  package example_commands_test
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"time"
     9  
    10  	"github.com/redis/go-redis/v9"
    11  )
    12  
    13  func ExampleClient_LPush_and_lrange() {
    14  	ctx := context.Background()
    15  
    16  	rdb := redis.NewClient(&redis.Options{
    17  		Addr:     "localhost:6379",
    18  		Password: "", // no password docs
    19  		DB:       0,  // use default DB
    20  	})
    21  
    22  	// HIDE_END
    23  
    24  	// REMOVE_START
    25  	errFlush := rdb.FlushDB(ctx).Err() // Clear the database before each test
    26  	if errFlush != nil {
    27  		panic(errFlush)
    28  	}
    29  	// REMOVE_END
    30  
    31  	listSize, err := rdb.LPush(ctx, "my_bikes", "bike:1", "bike:2").Result()
    32  	if err != nil {
    33  		panic(err)
    34  	}
    35  
    36  	fmt.Println(listSize)
    37  	time.Sleep(10 * time.Millisecond) // Simulate some delay
    38  
    39  	value, err := rdb.LRange(ctx, "my_bikes", 0, -1).Result()
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  	fmt.Println(value)
    44  	// HIDE_START
    45  
    46  	// Output: 2
    47  	// [bike:2 bike:1]
    48  }
    49  
    50  // HIDE_END
    51  

View as plain text