...

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

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

     1  // EXAMPLE: list_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_queue() {
    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, "bikes:repairs")
    27  	// REMOVE_END
    28  
    29  	// STEP_START queue
    30  	res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
    31  
    32  	if err != nil {
    33  		panic(err)
    34  	}
    35  
    36  	fmt.Println(res1) // >>> 1
    37  
    38  	res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
    39  
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  
    44  	fmt.Println(res2) // >>> 2
    45  
    46  	res3, err := rdb.RPop(ctx, "bikes:repairs").Result()
    47  
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  
    52  	fmt.Println(res3) // >>> bike:1
    53  
    54  	res4, err := rdb.RPop(ctx, "bikes:repairs").Result()
    55  
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  
    60  	fmt.Println(res4) // >>> bike:2
    61  	// STEP_END
    62  
    63  	// Output:
    64  	// 1
    65  	// 2
    66  	// bike:1
    67  	// bike:2
    68  }
    69  
    70  func ExampleClient_stack() {
    71  	ctx := context.Background()
    72  
    73  	rdb := redis.NewClient(&redis.Options{
    74  		Addr:     "localhost:6379",
    75  		Password: "", // no password docs
    76  		DB:       0,  // use default DB
    77  	})
    78  
    79  	// REMOVE_START
    80  	// start with fresh database
    81  	rdb.FlushDB(ctx)
    82  	rdb.Del(ctx, "bikes:repairs")
    83  	// REMOVE_END
    84  
    85  	// STEP_START stack
    86  	res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
    87  
    88  	if err != nil {
    89  		panic(err)
    90  	}
    91  
    92  	fmt.Println(res5) // >>> 1
    93  
    94  	res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
    95  
    96  	if err != nil {
    97  		panic(err)
    98  	}
    99  
   100  	fmt.Println(res6) // >>> 2
   101  
   102  	res7, err := rdb.LPop(ctx, "bikes:repairs").Result()
   103  
   104  	if err != nil {
   105  		panic(err)
   106  	}
   107  
   108  	fmt.Println(res7) // >>> bike:2
   109  
   110  	res8, err := rdb.LPop(ctx, "bikes:repairs").Result()
   111  
   112  	if err != nil {
   113  		panic(err)
   114  	}
   115  
   116  	fmt.Println(res8) // >>> bike:1
   117  	// STEP_END
   118  
   119  	// Output:
   120  	// 1
   121  	// 2
   122  	// bike:2
   123  	// bike:1
   124  }
   125  
   126  func ExampleClient_llen() {
   127  	ctx := context.Background()
   128  
   129  	rdb := redis.NewClient(&redis.Options{
   130  		Addr:     "localhost:6379",
   131  		Password: "", // no password docs
   132  		DB:       0,  // use default DB
   133  	})
   134  
   135  	// REMOVE_START
   136  	// start with fresh database
   137  	rdb.FlushDB(ctx)
   138  	rdb.Del(ctx, "bikes:repairs")
   139  	// REMOVE_END
   140  
   141  	// STEP_START llen
   142  	res9, err := rdb.LLen(ctx, "bikes:repairs").Result()
   143  
   144  	if err != nil {
   145  		panic(err)
   146  	}
   147  
   148  	fmt.Println(res9) // >>> 0
   149  	// STEP_END
   150  
   151  	// Output:
   152  	// 0
   153  }
   154  
   155  func ExampleClient_lmove_lrange() {
   156  	ctx := context.Background()
   157  
   158  	rdb := redis.NewClient(&redis.Options{
   159  		Addr:     "localhost:6379",
   160  		Password: "", // no password docs
   161  		DB:       0,  // use default DB
   162  	})
   163  
   164  	// REMOVE_START
   165  	// start with fresh database
   166  	rdb.FlushDB(ctx)
   167  	rdb.Del(ctx, "bikes:repairs")
   168  	rdb.Del(ctx, "bikes:finished")
   169  	// REMOVE_END
   170  
   171  	// STEP_START lmove_lrange
   172  	res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
   173  
   174  	if err != nil {
   175  		panic(err)
   176  	}
   177  
   178  	fmt.Println(res10) // >>> 1
   179  
   180  	res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
   181  
   182  	if err != nil {
   183  		panic(err)
   184  	}
   185  
   186  	fmt.Println(res11) // >>> 2
   187  
   188  	res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result()
   189  
   190  	if err != nil {
   191  		panic(err)
   192  	}
   193  
   194  	fmt.Println(res12) // >>> bike:2
   195  
   196  	res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
   197  
   198  	if err != nil {
   199  		panic(err)
   200  	}
   201  
   202  	fmt.Println(res13) // >>> [bike:1]
   203  
   204  	res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result()
   205  
   206  	if err != nil {
   207  		panic(err)
   208  	}
   209  
   210  	fmt.Println(res14) // >>> [bike:2]
   211  	// STEP_END
   212  
   213  	// Output:
   214  	// 1
   215  	// 2
   216  	// bike:2
   217  	// [bike:1]
   218  	// [bike:2]
   219  }
   220  
   221  func ExampleClient_lpush_rpush() {
   222  	ctx := context.Background()
   223  
   224  	rdb := redis.NewClient(&redis.Options{
   225  		Addr:     "localhost:6379",
   226  		Password: "", // no password docs
   227  		DB:       0,  // use default DB
   228  	})
   229  
   230  	// REMOVE_START
   231  	// start with fresh database
   232  	rdb.FlushDB(ctx)
   233  	rdb.Del(ctx, "bikes:repairs")
   234  	// REMOVE_END
   235  
   236  	// STEP_START lpush_rpush
   237  	res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result()
   238  
   239  	if err != nil {
   240  		panic(err)
   241  	}
   242  
   243  	fmt.Println(res15) // >>> 1
   244  
   245  	res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result()
   246  
   247  	if err != nil {
   248  		panic(err)
   249  	}
   250  
   251  	fmt.Println(res16) // >>> 2
   252  
   253  	res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result()
   254  
   255  	if err != nil {
   256  		panic(err)
   257  	}
   258  
   259  	fmt.Println(res17) // >>> 3
   260  
   261  	res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
   262  
   263  	if err != nil {
   264  		panic(err)
   265  	}
   266  
   267  	fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2]
   268  	// STEP_END
   269  
   270  	// Output:
   271  	// 1
   272  	// 2
   273  	// 3
   274  	// [bike:important_bike bike:1 bike:2]
   275  }
   276  
   277  func ExampleClient_variadic() {
   278  	ctx := context.Background()
   279  
   280  	rdb := redis.NewClient(&redis.Options{
   281  		Addr:     "localhost:6379",
   282  		Password: "", // no password docs
   283  		DB:       0,  // use default DB
   284  	})
   285  
   286  	// REMOVE_START
   287  	// start with fresh database
   288  	rdb.FlushDB(ctx)
   289  	rdb.Del(ctx, "bikes:repairs")
   290  	// REMOVE_END
   291  
   292  	// STEP_START variadic
   293  	res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
   294  
   295  	if err != nil {
   296  		panic(err)
   297  	}
   298  
   299  	fmt.Println(res19) // >>> 3
   300  
   301  	res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result()
   302  
   303  	if err != nil {
   304  		panic(err)
   305  	}
   306  
   307  	fmt.Println(res20) // >>> 5
   308  
   309  	res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
   310  
   311  	if err != nil {
   312  		panic(err)
   313  	}
   314  
   315  	fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3]
   316  	// STEP_END
   317  
   318  	// Output:
   319  	// 3
   320  	// 5
   321  	// [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3]
   322  }
   323  
   324  func ExampleClient_lpop_rpop() {
   325  	ctx := context.Background()
   326  
   327  	rdb := redis.NewClient(&redis.Options{
   328  		Addr:     "localhost:6379",
   329  		Password: "", // no password docs
   330  		DB:       0,  // use default DB
   331  	})
   332  
   333  	// REMOVE_START
   334  	// start with fresh database
   335  	rdb.FlushDB(ctx)
   336  	rdb.Del(ctx, "bikes:repairs")
   337  	// REMOVE_END
   338  
   339  	// STEP_START lpop_rpop
   340  	res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
   341  
   342  	if err != nil {
   343  		panic(err)
   344  	}
   345  
   346  	fmt.Println(res22) // >>> 3
   347  
   348  	res23, err := rdb.RPop(ctx, "bikes:repairs").Result()
   349  
   350  	if err != nil {
   351  		panic(err)
   352  	}
   353  
   354  	fmt.Println(res23) // >>> bike:3
   355  
   356  	res24, err := rdb.LPop(ctx, "bikes:repairs").Result()
   357  
   358  	if err != nil {
   359  		panic(err)
   360  	}
   361  
   362  	fmt.Println(res24) // >>> bike:1
   363  
   364  	res25, err := rdb.RPop(ctx, "bikes:repairs").Result()
   365  
   366  	if err != nil {
   367  		panic(err)
   368  	}
   369  
   370  	fmt.Println(res25) // >>> bike:2
   371  
   372  	res26, err := rdb.RPop(ctx, "bikes:repairs").Result()
   373  
   374  	if err != nil {
   375  		fmt.Println(err) // >>> redis: nil
   376  	}
   377  
   378  	fmt.Println(res26) // >>> <empty string>
   379  
   380  	// STEP_END
   381  
   382  	// Output:
   383  	// 3
   384  	// bike:3
   385  	// bike:1
   386  	// bike:2
   387  	// redis: nil
   388  	//
   389  }
   390  
   391  func ExampleClient_ltrim() {
   392  	ctx := context.Background()
   393  
   394  	rdb := redis.NewClient(&redis.Options{
   395  		Addr:     "localhost:6379",
   396  		Password: "", // no password docs
   397  		DB:       0,  // use default DB
   398  	})
   399  
   400  	// REMOVE_START
   401  	// start with fresh database
   402  	rdb.FlushDB(ctx)
   403  	rdb.Del(ctx, "bikes:repairs")
   404  	// REMOVE_END
   405  
   406  	// STEP_START ltrim
   407  	res27, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
   408  
   409  	if err != nil {
   410  		panic(err)
   411  	}
   412  
   413  	fmt.Println(res27) // >>> 5
   414  
   415  	res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
   416  
   417  	if err != nil {
   418  		panic(err)
   419  	}
   420  
   421  	fmt.Println(res28) // >>> OK
   422  
   423  	res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
   424  
   425  	if err != nil {
   426  		panic(err)
   427  	}
   428  
   429  	fmt.Println(res29) // >>> [bike:1 bike:2 bike:3]
   430  	// STEP_END
   431  
   432  	// Output:
   433  	// 5
   434  	// OK
   435  	// [bike:1 bike:2 bike:3]
   436  }
   437  
   438  func ExampleClient_ltrim_end_of_list() {
   439  	ctx := context.Background()
   440  
   441  	rdb := redis.NewClient(&redis.Options{
   442  		Addr:     "localhost:6379",
   443  		Password: "", // no password docs
   444  		DB:       0,  // use default DB
   445  	})
   446  
   447  	// REMOVE_START
   448  	// start with fresh database
   449  	rdb.FlushDB(ctx)
   450  	rdb.Del(ctx, "bikes:repairs")
   451  	// REMOVE_END
   452  
   453  	// STEP_START ltrim_end_of_list
   454  	res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
   455  
   456  	if err != nil {
   457  		panic(err)
   458  	}
   459  
   460  	fmt.Println(res30) // >>> 5
   461  
   462  	res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result()
   463  
   464  	if err != nil {
   465  		panic(err)
   466  	}
   467  
   468  	fmt.Println(res31) // >>> OK
   469  
   470  	res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
   471  
   472  	if err != nil {
   473  		panic(err)
   474  	}
   475  
   476  	fmt.Println(res32) // >>> [bike:3 bike:4 bike:5]
   477  	// STEP_END
   478  
   479  	// Output:
   480  	// 5
   481  	// OK
   482  	// [bike:3 bike:4 bike:5]
   483  }
   484  
   485  func ExampleClient_brpop() {
   486  	ctx := context.Background()
   487  
   488  	rdb := redis.NewClient(&redis.Options{
   489  		Addr:     "localhost:6379",
   490  		Password: "", // no password docs
   491  		DB:       0,  // use default DB
   492  	})
   493  
   494  	// REMOVE_START
   495  	// start with fresh database
   496  	rdb.FlushDB(ctx)
   497  	rdb.Del(ctx, "bikes:repairs")
   498  	// REMOVE_END
   499  
   500  	// STEP_START brpop
   501  	res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result()
   502  
   503  	if err != nil {
   504  		panic(err)
   505  	}
   506  
   507  	fmt.Println(res33) // >>> 2
   508  
   509  	res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
   510  
   511  	if err != nil {
   512  		panic(err)
   513  	}
   514  
   515  	fmt.Println(res34) // >>> [bikes:repairs bike:2]
   516  
   517  	res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
   518  
   519  	if err != nil {
   520  		panic(err)
   521  	}
   522  
   523  	fmt.Println(res35) // >>> [bikes:repairs bike:1]
   524  
   525  	res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
   526  
   527  	if err != nil {
   528  		fmt.Println(err) // >>> redis: nil
   529  	}
   530  
   531  	fmt.Println(res36) // >>> []
   532  	// STEP_END
   533  
   534  	// Output:
   535  	// 2
   536  	// [bikes:repairs bike:2]
   537  	// [bikes:repairs bike:1]
   538  	// redis: nil
   539  	// []
   540  }
   541  
   542  func ExampleClient_rule1() {
   543  	ctx := context.Background()
   544  
   545  	rdb := redis.NewClient(&redis.Options{
   546  		Addr:     "localhost:6379",
   547  		Password: "", // no password docs
   548  		DB:       0,  // use default DB
   549  	})
   550  
   551  	// REMOVE_START
   552  	// start with fresh database
   553  	rdb.FlushDB(ctx)
   554  	rdb.Del(ctx, "new_bikes")
   555  	// REMOVE_END
   556  
   557  	// STEP_START rule_1
   558  	res37, err := rdb.Del(ctx, "new_bikes").Result()
   559  
   560  	if err != nil {
   561  		panic(err)
   562  	}
   563  
   564  	fmt.Println(res37) // >>> 0
   565  
   566  	res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result()
   567  
   568  	if err != nil {
   569  		panic(err)
   570  	}
   571  
   572  	fmt.Println(res38) // >>> 3
   573  	// STEP_END
   574  
   575  	// Output:
   576  	// 0
   577  	// 3
   578  }
   579  
   580  func ExampleClient_rule11() {
   581  	ctx := context.Background()
   582  
   583  	rdb := redis.NewClient(&redis.Options{
   584  		Addr:     "localhost:6379",
   585  		Password: "", // no password docs
   586  		DB:       0,  // use default DB
   587  	})
   588  
   589  	// REMOVE_START
   590  	rdb.Del(ctx, "new_bikes")
   591  	// REMOVE_END
   592  
   593  	// STEP_START rule_1.1
   594  	res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result()
   595  
   596  	if err != nil {
   597  		panic(err)
   598  	}
   599  
   600  	fmt.Println(res39) // >>> OK
   601  
   602  	res40, err := rdb.Type(ctx, "new_bikes").Result()
   603  
   604  	if err != nil {
   605  		panic(err)
   606  	}
   607  
   608  	fmt.Println(res40) // >>> string
   609  
   610  	res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result()
   611  
   612  	if err != nil {
   613  		fmt.Println(err)
   614  		// >>> WRONGTYPE Operation against a key holding the wrong kind of value
   615  	}
   616  
   617  	fmt.Println(res41)
   618  	// STEP_END
   619  
   620  	// Output:
   621  	// OK
   622  	// string
   623  	// WRONGTYPE Operation against a key holding the wrong kind of value
   624  	// 0
   625  }
   626  
   627  func ExampleClient_rule2() {
   628  	ctx := context.Background()
   629  
   630  	rdb := redis.NewClient(&redis.Options{
   631  		Addr:     "localhost:6379",
   632  		Password: "", // no password docs
   633  		DB:       0,  // use default DB
   634  	})
   635  
   636  	// REMOVE_START
   637  	rdb.Del(ctx, "bikes:repairs")
   638  	// REMOVE_END
   639  
   640  	// STEP_START rule_2
   641  	res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
   642  
   643  	if err != nil {
   644  		panic(err)
   645  	}
   646  
   647  	fmt.Println(res42) // >>> 3
   648  
   649  	res43, err := rdb.Exists(ctx, "bikes:repairs").Result()
   650  
   651  	if err != nil {
   652  		panic(err)
   653  	}
   654  
   655  	fmt.Println(res43) // >>> 1
   656  
   657  	res44, err := rdb.LPop(ctx, "bikes:repairs").Result()
   658  
   659  	if err != nil {
   660  		panic(err)
   661  	}
   662  
   663  	fmt.Println(res44) // >>> bike:3
   664  
   665  	res45, err := rdb.LPop(ctx, "bikes:repairs").Result()
   666  
   667  	if err != nil {
   668  		panic(err)
   669  	}
   670  
   671  	fmt.Println(res45) // >>> bike:2
   672  
   673  	res46, err := rdb.LPop(ctx, "bikes:repairs").Result()
   674  
   675  	if err != nil {
   676  		panic(err)
   677  	}
   678  
   679  	fmt.Println(res46) // >>> bike:1
   680  
   681  	res47, err := rdb.Exists(ctx, "bikes:repairs").Result()
   682  
   683  	if err != nil {
   684  		panic(err)
   685  	}
   686  
   687  	fmt.Println(res47) // >>> 0
   688  	// STEP_END
   689  
   690  	// Output:
   691  	// 3
   692  	// 1
   693  	// bike:3
   694  	// bike:2
   695  	// bike:1
   696  	// 0
   697  }
   698  
   699  func ExampleClient_rule3() {
   700  	ctx := context.Background()
   701  
   702  	rdb := redis.NewClient(&redis.Options{
   703  		Addr:     "localhost:6379",
   704  		Password: "", // no password docs
   705  		DB:       0,  // use default DB
   706  	})
   707  
   708  	// REMOVE_START
   709  	rdb.Del(ctx, "bikes:repairs")
   710  	// REMOVE_END
   711  
   712  	// STEP_START rule_3
   713  	res48, err := rdb.Del(ctx, "bikes:repairs").Result()
   714  
   715  	if err != nil {
   716  		panic(err)
   717  	}
   718  
   719  	fmt.Println(res48) // >>> 0
   720  
   721  	res49, err := rdb.LLen(ctx, "bikes:repairs").Result()
   722  
   723  	if err != nil {
   724  		panic(err)
   725  	}
   726  
   727  	fmt.Println(res49) // >>> 0
   728  
   729  	res50, err := rdb.LPop(ctx, "bikes:repairs").Result()
   730  
   731  	if err != nil {
   732  		fmt.Println(err) // >>> redis: nil
   733  	}
   734  
   735  	fmt.Println(res50) // >>> <empty string>
   736  	// STEP_END
   737  
   738  	// Output:
   739  	// 0
   740  	// 0
   741  	// redis: nil
   742  	//
   743  }
   744  
   745  func ExampleClient_ltrim1() {
   746  	ctx := context.Background()
   747  
   748  	rdb := redis.NewClient(&redis.Options{
   749  		Addr:     "localhost:6379",
   750  		Password: "", // no password docs
   751  		DB:       0,  // use default DB
   752  	})
   753  
   754  	// REMOVE_START
   755  	rdb.Del(ctx, "bikes:repairs")
   756  	// REMOVE_END
   757  
   758  	// STEP_START ltrim.1
   759  	res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
   760  
   761  	if err != nil {
   762  		panic(err)
   763  	}
   764  
   765  	fmt.Println(res51) // >>> 5
   766  
   767  	res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
   768  
   769  	if err != nil {
   770  		panic(err)
   771  	}
   772  
   773  	fmt.Println(res52) // >>> OK
   774  
   775  	res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
   776  
   777  	if err != nil {
   778  		panic(err)
   779  	}
   780  
   781  	fmt.Println(res53) // >>> [bike:5 bike:4 bike:3]
   782  	// STEP_END
   783  
   784  	// Output:
   785  	// 5
   786  	// OK
   787  	// [bike:5 bike:4 bike:3]
   788  }
   789  

View as plain text