...

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

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

     1  // EXAMPLE: cmds_list
     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_llen() {
    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, "mylist")
    25  	// REMOVE_END
    26  
    27  	// STEP_START llen
    28  	lPushResult1, err := rdb.LPush(ctx, "mylist", "World").Result()
    29  
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  
    34  	fmt.Println(lPushResult1) // >>> 1
    35  
    36  	lPushResult2, err := rdb.LPush(ctx, "mylist", "Hello").Result()
    37  
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  
    42  	fmt.Println(lPushResult2) // >>> 2
    43  
    44  	lLenResult, err := rdb.LLen(ctx, "mylist").Result()
    45  
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  
    50  	fmt.Println(lLenResult) // >>> 2
    51  	// STEP_END
    52  
    53  	// Output:
    54  	// 1
    55  	// 2
    56  	// 2
    57  }
    58  func ExampleClient_cmd_lpop() {
    59  	ctx := context.Background()
    60  
    61  	rdb := redis.NewClient(&redis.Options{
    62  		Addr:     "localhost:6379",
    63  		Password: "", // no password docs
    64  		DB:       0,  // use default DB
    65  	})
    66  
    67  	// REMOVE_START
    68  	rdb.Del(ctx, "mylist")
    69  	// REMOVE_END
    70  
    71  	// STEP_START lpop
    72  	RPushResult, err := rdb.RPush(ctx,
    73  		"mylist", "one", "two", "three", "four", "five",
    74  	).Result()
    75  
    76  	if err != nil {
    77  		panic(err)
    78  	}
    79  
    80  	fmt.Println(RPushResult) // >>> 5
    81  
    82  	lPopResult, err := rdb.LPop(ctx, "mylist").Result()
    83  
    84  	if err != nil {
    85  		panic(err)
    86  	}
    87  
    88  	fmt.Println(lPopResult) // >>> one
    89  
    90  	lPopCountResult, err := rdb.LPopCount(ctx, "mylist", 2).Result()
    91  
    92  	if err != nil {
    93  		panic(err)
    94  	}
    95  
    96  	fmt.Println(lPopCountResult) // >>> [two three]
    97  
    98  	lRangeResult, err := rdb.LRange(ctx, "mylist", 0, -1).Result()
    99  
   100  	if err != nil {
   101  		panic(err)
   102  	}
   103  
   104  	fmt.Println(lRangeResult) // >>> [four five]
   105  	// STEP_END
   106  
   107  	// Output:
   108  	// 5
   109  	// one
   110  	// [two three]
   111  	// [four five]
   112  }
   113  
   114  func ExampleClient_cmd_lpush() {
   115  	ctx := context.Background()
   116  
   117  	rdb := redis.NewClient(&redis.Options{
   118  		Addr:     "localhost:6379",
   119  		Password: "", // no password docs
   120  		DB:       0,  // use default DB
   121  	})
   122  
   123  	// REMOVE_START
   124  	rdb.Del(ctx, "mylist")
   125  	// REMOVE_END
   126  
   127  	// STEP_START lpush
   128  	lPushResult1, err := rdb.LPush(ctx, "mylist", "World").Result()
   129  
   130  	if err != nil {
   131  		panic(err)
   132  	}
   133  
   134  	fmt.Println(lPushResult1) // >>> 1
   135  
   136  	lPushResult2, err := rdb.LPush(ctx, "mylist", "Hello").Result()
   137  
   138  	if err != nil {
   139  		panic(err)
   140  	}
   141  
   142  	fmt.Println(lPushResult2) // >>> 2
   143  
   144  	lRangeResult, err := rdb.LRange(ctx, "mylist", 0, -1).Result()
   145  
   146  	if err != nil {
   147  		panic(err)
   148  	}
   149  
   150  	fmt.Println(lRangeResult) // >>> [Hello World]
   151  	// STEP_END
   152  
   153  	// Output:
   154  	// 1
   155  	// 2
   156  	// [Hello World]
   157  }
   158  
   159  func ExampleClient_cmd_lrange() {
   160  	ctx := context.Background()
   161  
   162  	rdb := redis.NewClient(&redis.Options{
   163  		Addr:     "localhost:6379",
   164  		Password: "", // no password docs
   165  		DB:       0,  // use default DB
   166  	})
   167  
   168  	// REMOVE_START
   169  	rdb.Del(ctx, "mylist")
   170  	// REMOVE_END
   171  
   172  	// STEP_START lrange
   173  	RPushResult, err := rdb.RPush(ctx, "mylist",
   174  		"one", "two", "three",
   175  	).Result()
   176  
   177  	if err != nil {
   178  		panic(err)
   179  	}
   180  
   181  	fmt.Println(RPushResult) // >>> 3
   182  
   183  	lRangeResult1, err := rdb.LRange(ctx, "mylist", 0, 0).Result()
   184  
   185  	if err != nil {
   186  		panic(err)
   187  	}
   188  
   189  	fmt.Println(lRangeResult1) // >>> [one]
   190  
   191  	lRangeResult2, err := rdb.LRange(ctx, "mylist", -3, 2).Result()
   192  
   193  	if err != nil {
   194  		panic(err)
   195  	}
   196  
   197  	fmt.Println(lRangeResult2) // >>> [one two three]
   198  
   199  	lRangeResult3, err := rdb.LRange(ctx, "mylist", -100, 100).Result()
   200  
   201  	if err != nil {
   202  		panic(err)
   203  	}
   204  
   205  	fmt.Println(lRangeResult3) // >>> [one two three]
   206  
   207  	lRangeResult4, err := rdb.LRange(ctx, "mylist", 5, 10).Result()
   208  
   209  	if err != nil {
   210  		panic(err)
   211  	}
   212  
   213  	fmt.Println(lRangeResult4) // >>> []
   214  	// STEP_END
   215  
   216  	// Output:
   217  	// 3
   218  	// [one]
   219  	// [one two three]
   220  	// [one two three]
   221  	// []
   222  }
   223  
   224  func ExampleClient_cmd_rpop() {
   225  	ctx := context.Background()
   226  
   227  	rdb := redis.NewClient(&redis.Options{
   228  		Addr:     "localhost:6379",
   229  		Password: "", // no password docs
   230  		DB:       0,  // use default DB
   231  	})
   232  
   233  	// REMOVE_START
   234  	rdb.Del(ctx, "mylist")
   235  	// REMOVE_END
   236  
   237  	// STEP_START rpop
   238  	rPushResult, err := rdb.RPush(ctx, "mylist",
   239  		"one", "two", "three", "four", "five",
   240  	).Result()
   241  
   242  	if err != nil {
   243  		panic(err)
   244  	}
   245  
   246  	fmt.Println(rPushResult) // >>> 5
   247  
   248  	rPopResult, err := rdb.RPop(ctx, "mylist").Result()
   249  
   250  	if err != nil {
   251  		panic(err)
   252  	}
   253  
   254  	fmt.Println(rPopResult) // >>> five
   255  
   256  	rPopCountResult, err := rdb.RPopCount(ctx, "mylist", 2).Result()
   257  
   258  	if err != nil {
   259  		panic(err)
   260  	}
   261  
   262  	fmt.Println(rPopCountResult) // >>> [four three]
   263  
   264  	lRangeResult, err := rdb.LRange(ctx, "mylist", 0, -1).Result()
   265  
   266  	if err != nil {
   267  		panic(err)
   268  	}
   269  
   270  	fmt.Println(lRangeResult) // >>> [one two]
   271  	// STEP_END
   272  
   273  	// Output:
   274  	// 5
   275  	// five
   276  	// [four three]
   277  	// [one two]
   278  }
   279  
   280  func ExampleClient_cmd_rpush() {
   281  	ctx := context.Background()
   282  
   283  	rdb := redis.NewClient(&redis.Options{
   284  		Addr:     "localhost:6379",
   285  		Password: "", // no password docs
   286  		DB:       0,  // use default DB
   287  	})
   288  
   289  	// REMOVE_START
   290  	rdb.Del(ctx, "mylist")
   291  	// REMOVE_END
   292  
   293  	// STEP_START rpush
   294  	rPushResult1, err := rdb.RPush(ctx, "mylist", "Hello").Result()
   295  
   296  	if err != nil {
   297  		panic(err)
   298  	}
   299  
   300  	fmt.Println(rPushResult1) // >>> 1
   301  
   302  	rPushResult2, err := rdb.RPush(ctx, "mylist", "World").Result()
   303  
   304  	if err != nil {
   305  		panic(err)
   306  	}
   307  
   308  	fmt.Println(rPushResult2) // >>> 2
   309  
   310  	lRangeResult, err := rdb.LRange(ctx, "mylist", 0, -1).Result()
   311  
   312  	if err != nil {
   313  		panic(err)
   314  	}
   315  
   316  	fmt.Println(lRangeResult) // >>> [Hello World]
   317  	// STEP_END
   318  
   319  	// Output:
   320  	// 1
   321  	// 2
   322  	// [Hello World]
   323  }
   324  

View as plain text