...

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

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

     1  // EXAMPLE: sets_tutorial
     2  // HIDE_START
     3  package example_commands_test
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"sort"
     9  
    10  	"github.com/redis/go-redis/v9"
    11  )
    12  
    13  // HIDE_END
    14  func ExampleClient_sadd() {
    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  	// start with fresh database
    25  	rdb.FlushDB(ctx)
    26  	rdb.Del(ctx, "bikes:racing:france")
    27  	rdb.Del(ctx, "bikes:racing:usa")
    28  	// REMOVE_END
    29  
    30  	// STEP_START sadd
    31  	res1, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1").Result()
    32  
    33  	if err != nil {
    34  		panic(err)
    35  	}
    36  
    37  	fmt.Println(res1) // >>> 1
    38  
    39  	res2, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1").Result()
    40  
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  
    45  	fmt.Println(res2) // >>> 0
    46  
    47  	res3, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:2", "bike:3").Result()
    48  
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	fmt.Println(res3) // >>> 2
    54  
    55  	res4, err := rdb.SAdd(ctx, "bikes:racing:usa", "bike:1", "bike:4").Result()
    56  
    57  	if err != nil {
    58  		panic(err)
    59  	}
    60  
    61  	fmt.Println(res4) // >>> 2
    62  	// STEP_END
    63  
    64  	// Output:
    65  	// 1
    66  	// 0
    67  	// 2
    68  	// 2
    69  }
    70  
    71  func ExampleClient_sismember() {
    72  	ctx := context.Background()
    73  
    74  	rdb := redis.NewClient(&redis.Options{
    75  		Addr:     "localhost:6379",
    76  		Password: "", // no password docs
    77  		DB:       0,  // use default DB
    78  	})
    79  
    80  	// REMOVE_START
    81  	// start with fresh database
    82  	rdb.FlushDB(ctx)
    83  	rdb.Del(ctx, "bikes:racing:france")
    84  	rdb.Del(ctx, "bikes:racing:usa")
    85  	// REMOVE_END
    86  
    87  	_, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3").Result()
    88  
    89  	if err != nil {
    90  		panic(err)
    91  	}
    92  
    93  	_, err = rdb.SAdd(ctx, "bikes:racing:usa", "bike:1", "bike:4").Result()
    94  
    95  	if err != nil {
    96  		panic(err)
    97  	}
    98  
    99  	// STEP_START sismember
   100  	res5, err := rdb.SIsMember(ctx, "bikes:racing:usa", "bike:1").Result()
   101  
   102  	if err != nil {
   103  		panic(err)
   104  	}
   105  
   106  	fmt.Println(res5) // >>> true
   107  
   108  	res6, err := rdb.SIsMember(ctx, "bikes:racing:usa", "bike:2").Result()
   109  
   110  	if err != nil {
   111  		panic(err)
   112  	}
   113  
   114  	fmt.Println(res6) // >>> false
   115  	// STEP_END
   116  
   117  	// Output:
   118  	// true
   119  	// false
   120  }
   121  
   122  func ExampleClient_sinter() {
   123  	ctx := context.Background()
   124  
   125  	rdb := redis.NewClient(&redis.Options{
   126  		Addr:     "localhost:6379",
   127  		Password: "", // no password docs
   128  		DB:       0,  // use default DB
   129  	})
   130  
   131  	// REMOVE_START
   132  	// start with fresh database
   133  	rdb.FlushDB(ctx)
   134  	rdb.Del(ctx, "bikes:racing:france")
   135  	rdb.Del(ctx, "bikes:racing:usa")
   136  	// REMOVE_END
   137  
   138  	_, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3").Result()
   139  
   140  	if err != nil {
   141  		panic(err)
   142  	}
   143  
   144  	_, err = rdb.SAdd(ctx, "bikes:racing:usa", "bike:1", "bike:4").Result()
   145  
   146  	if err != nil {
   147  		panic(err)
   148  	}
   149  
   150  	// STEP_START sinter
   151  	res7, err := rdb.SInter(ctx, "bikes:racing:france", "bikes:racing:usa").Result()
   152  
   153  	if err != nil {
   154  		panic(err)
   155  	}
   156  
   157  	fmt.Println(res7) // >>> [bike:1]
   158  	// STEP_END
   159  
   160  	// Output:
   161  	// [bike:1]
   162  }
   163  
   164  func ExampleClient_scard() {
   165  	ctx := context.Background()
   166  
   167  	rdb := redis.NewClient(&redis.Options{
   168  		Addr:     "localhost:6379",
   169  		Password: "", // no password docs
   170  		DB:       0,  // use default DB
   171  	})
   172  
   173  	// REMOVE_START
   174  	// start with fresh database
   175  	rdb.FlushDB(ctx)
   176  	rdb.Del(ctx, "bikes:racing:france")
   177  	// REMOVE_END
   178  
   179  	_, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3").Result()
   180  
   181  	if err != nil {
   182  		panic(err)
   183  	}
   184  
   185  	// STEP_START scard
   186  	res8, err := rdb.SCard(ctx, "bikes:racing:france").Result()
   187  
   188  	if err != nil {
   189  		panic(err)
   190  	}
   191  
   192  	fmt.Println(res8) // >>> 3
   193  	// STEP_END
   194  
   195  	// Output:
   196  	// 3
   197  }
   198  
   199  func ExampleClient_saddsmembers() {
   200  	ctx := context.Background()
   201  
   202  	rdb := redis.NewClient(&redis.Options{
   203  		Addr:     "localhost:6379",
   204  		Password: "", // no password docs
   205  		DB:       0,  // use default DB
   206  	})
   207  
   208  	// REMOVE_START
   209  	// start with fresh database
   210  	rdb.FlushDB(ctx)
   211  	rdb.Del(ctx, "bikes:racing:france")
   212  	// REMOVE_END
   213  
   214  	// STEP_START sadd_smembers
   215  	res9, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3").Result()
   216  
   217  	if err != nil {
   218  		panic(err)
   219  	}
   220  
   221  	fmt.Println(res9) // >>> 3
   222  
   223  	res10, err := rdb.SMembers(ctx, "bikes:racing:france").Result()
   224  
   225  	if err != nil {
   226  		panic(err)
   227  	}
   228  
   229  	// Sort the strings in the slice to make sure the output is lexicographical
   230  	sort.Strings(res10)
   231  
   232  	fmt.Println(res10) // >>> [bike:1 bike:2 bike:3]
   233  	// STEP_END
   234  
   235  	// Output:
   236  	// 3
   237  	// [bike:1 bike:2 bike:3]
   238  }
   239  
   240  func ExampleClient_smismember() {
   241  	ctx := context.Background()
   242  
   243  	rdb := redis.NewClient(&redis.Options{
   244  		Addr:     "localhost:6379",
   245  		Password: "", // no password docs
   246  		DB:       0,  // use default DB
   247  	})
   248  
   249  	// REMOVE_START
   250  	// start with fresh database
   251  	rdb.FlushDB(ctx)
   252  	rdb.Del(ctx, "bikes:racing:france")
   253  	// REMOVE_END
   254  
   255  	_, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3").Result()
   256  
   257  	if err != nil {
   258  		panic(err)
   259  	}
   260  
   261  	// STEP_START smismember
   262  	res11, err := rdb.SIsMember(ctx, "bikes:racing:france", "bike:1").Result()
   263  
   264  	if err != nil {
   265  		panic(err)
   266  	}
   267  
   268  	fmt.Println(res11) // >>> true
   269  
   270  	res12, err := rdb.SMIsMember(ctx, "bikes:racing:france", "bike:2", "bike:3", "bike:4").Result()
   271  
   272  	if err != nil {
   273  		panic(err)
   274  	}
   275  
   276  	fmt.Println(res12) // >>> [true true false]
   277  	// STEP_END
   278  
   279  	// Output:
   280  	// true
   281  	// [true true false]
   282  }
   283  
   284  func ExampleClient_sdiff() {
   285  	ctx := context.Background()
   286  
   287  	rdb := redis.NewClient(&redis.Options{
   288  		Addr:     "localhost:6379",
   289  		Password: "", // no password docs
   290  		DB:       0,  // use default DB
   291  	})
   292  
   293  	// REMOVE_START
   294  	// start with fresh database
   295  	rdb.FlushDB(ctx)
   296  	rdb.Del(ctx, "bikes:racing:france")
   297  	rdb.Del(ctx, "bikes:racing:usa")
   298  	// REMOVE_END
   299  
   300  	// STEP_START sdiff
   301  	_, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3").Result()
   302  
   303  	if err != nil {
   304  		panic(err)
   305  	}
   306  
   307  	_, err = rdb.SAdd(ctx, "bikes:racing:usa", "bike:1", "bike:4").Result()
   308  
   309  	res13, err := rdb.SDiff(ctx, "bikes:racing:france", "bikes:racing:usa").Result()
   310  
   311  	if err != nil {
   312  		panic(err)
   313  	}
   314  
   315  	// Sort the strings in the slice to make sure the output is lexicographical
   316  	sort.Strings(res13)
   317  
   318  	fmt.Println(res13) // >>> [bike:2 bike:3]
   319  	// STEP_END
   320  
   321  	// Output:
   322  	// [bike:2 bike:3]
   323  }
   324  
   325  func ExampleClient_multisets() {
   326  	ctx := context.Background()
   327  
   328  	rdb := redis.NewClient(&redis.Options{
   329  		Addr:     "localhost:6379",
   330  		Password: "", // no password docs
   331  		DB:       0,  // use default DB
   332  	})
   333  
   334  	// REMOVE_START
   335  	// start with fresh database
   336  	rdb.FlushDB(ctx)
   337  	rdb.Del(ctx, "bikes:racing:france")
   338  	rdb.Del(ctx, "bikes:racing:usa")
   339  	rdb.Del(ctx, "bikes:racing:italy")
   340  	// REMOVE_END
   341  
   342  	// STEP_START multisets
   343  	_, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3").Result()
   344  
   345  	if err != nil {
   346  		panic(err)
   347  	}
   348  
   349  	_, err = rdb.SAdd(ctx, "bikes:racing:usa", "bike:1", "bike:4").Result()
   350  
   351  	if err != nil {
   352  		panic(err)
   353  	}
   354  
   355  	_, err = rdb.SAdd(ctx, "bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4").Result()
   356  
   357  	if err != nil {
   358  		panic(err)
   359  	}
   360  
   361  	res14, err := rdb.SInter(ctx, "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy").Result()
   362  
   363  	if err != nil {
   364  		panic(err)
   365  	}
   366  
   367  	fmt.Println(res14) // >>> [bike:1]
   368  
   369  	res15, err := rdb.SUnion(ctx, "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy").Result()
   370  
   371  	if err != nil {
   372  		panic(err)
   373  	}
   374  
   375  	// Sort the strings in the slice to make sure the output is lexicographical
   376  	sort.Strings(res15)
   377  
   378  	fmt.Println(res15) // >>> [bike:1 bike:2 bike:3 bike:4]
   379  
   380  	res16, err := rdb.SDiff(ctx, "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy").Result()
   381  
   382  	if err != nil {
   383  		panic(err)
   384  	}
   385  
   386  	fmt.Println(res16) // >>> []
   387  
   388  	res17, err := rdb.SDiff(ctx, "bikes:racing:usa", "bikes:racing:france").Result()
   389  
   390  	if err != nil {
   391  		panic(err)
   392  	}
   393  
   394  	fmt.Println(res17) // >>> [bike:4]
   395  
   396  	res18, err := rdb.SDiff(ctx, "bikes:racing:france", "bikes:racing:usa").Result()
   397  
   398  	if err != nil {
   399  		panic(err)
   400  	}
   401  
   402  	// Sort the strings in the slice to make sure the output is lexicographical
   403  	sort.Strings(res18)
   404  
   405  	fmt.Println(res18) // >>> [bike:2 bike:3]
   406  	// STEP_END
   407  
   408  	// Output:
   409  	// [bike:1]
   410  	// [bike:1 bike:2 bike:3 bike:4]
   411  	// []
   412  	// [bike:4]
   413  	// [bike:2 bike:3]
   414  }
   415  
   416  func ExampleClient_srem() {
   417  	ctx := context.Background()
   418  
   419  	rdb := redis.NewClient(&redis.Options{
   420  		Addr:     "localhost:6379",
   421  		Password: "", // no password docs
   422  		DB:       0,  // use default DB
   423  	})
   424  
   425  	// REMOVE_START
   426  	// start with fresh database
   427  	rdb.FlushDB(ctx)
   428  	rdb.Del(ctx, "bikes:racing:france")
   429  	// REMOVE_END
   430  
   431  	// STEP_START srem
   432  	_, err := rdb.SAdd(ctx, "bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
   433  
   434  	if err != nil {
   435  		panic(err)
   436  	}
   437  
   438  	res19, err := rdb.SRem(ctx, "bikes:racing:france", "bike:1").Result()
   439  
   440  	if err != nil {
   441  		panic(err)
   442  	}
   443  
   444  	fmt.Println(res19) // >>> 1
   445  
   446  	res20, err := rdb.SPop(ctx, "bikes:racing:france").Result()
   447  
   448  	if err != nil {
   449  		panic(err)
   450  	}
   451  
   452  	fmt.Println(res20) // >>> <random element>
   453  
   454  	res21, err := rdb.SMembers(ctx, "bikes:racing:france").Result()
   455  
   456  	if err != nil {
   457  		panic(err)
   458  	}
   459  
   460  	fmt.Println(res21) // >>> <remaining elements>
   461  
   462  	res22, err := rdb.SRandMember(ctx, "bikes:racing:france").Result()
   463  
   464  	if err != nil {
   465  		panic(err)
   466  	}
   467  
   468  	fmt.Println(res22) // >>> <random element>
   469  	// STEP_END
   470  
   471  	// Testable examples not available because the test output
   472  	// is not deterministic.
   473  }
   474  

View as plain text