1
2
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
14 func ExampleClient_sadd() {
15 ctx := context.Background()
16
17 rdb := redis.NewClient(&redis.Options{
18 Addr: "localhost:6379",
19 Password: "",
20 DB: 0,
21 })
22
23
24
25 rdb.FlushDB(ctx)
26 rdb.Del(ctx, "bikes:racing:france")
27 rdb.Del(ctx, "bikes:racing:usa")
28
29
30
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)
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)
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)
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)
62
63
64
65
66
67
68
69 }
70
71 func ExampleClient_sismember() {
72 ctx := context.Background()
73
74 rdb := redis.NewClient(&redis.Options{
75 Addr: "localhost:6379",
76 Password: "",
77 DB: 0,
78 })
79
80
81
82 rdb.FlushDB(ctx)
83 rdb.Del(ctx, "bikes:racing:france")
84 rdb.Del(ctx, "bikes:racing:usa")
85
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
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)
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)
115
116
117
118
119
120 }
121
122 func ExampleClient_sinter() {
123 ctx := context.Background()
124
125 rdb := redis.NewClient(&redis.Options{
126 Addr: "localhost:6379",
127 Password: "",
128 DB: 0,
129 })
130
131
132
133 rdb.FlushDB(ctx)
134 rdb.Del(ctx, "bikes:racing:france")
135 rdb.Del(ctx, "bikes:racing:usa")
136
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
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)
158
159
160
161
162 }
163
164 func ExampleClient_scard() {
165 ctx := context.Background()
166
167 rdb := redis.NewClient(&redis.Options{
168 Addr: "localhost:6379",
169 Password: "",
170 DB: 0,
171 })
172
173
174
175 rdb.FlushDB(ctx)
176 rdb.Del(ctx, "bikes:racing:france")
177
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
186 res8, err := rdb.SCard(ctx, "bikes:racing:france").Result()
187
188 if err != nil {
189 panic(err)
190 }
191
192 fmt.Println(res8)
193
194
195
196
197 }
198
199 func ExampleClient_saddsmembers() {
200 ctx := context.Background()
201
202 rdb := redis.NewClient(&redis.Options{
203 Addr: "localhost:6379",
204 Password: "",
205 DB: 0,
206 })
207
208
209
210 rdb.FlushDB(ctx)
211 rdb.Del(ctx, "bikes:racing:france")
212
213
214
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)
222
223 res10, err := rdb.SMembers(ctx, "bikes:racing:france").Result()
224
225 if err != nil {
226 panic(err)
227 }
228
229
230 sort.Strings(res10)
231
232 fmt.Println(res10)
233
234
235
236
237
238 }
239
240 func ExampleClient_smismember() {
241 ctx := context.Background()
242
243 rdb := redis.NewClient(&redis.Options{
244 Addr: "localhost:6379",
245 Password: "",
246 DB: 0,
247 })
248
249
250
251 rdb.FlushDB(ctx)
252 rdb.Del(ctx, "bikes:racing:france")
253
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
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)
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)
277
278
279
280
281
282 }
283
284 func ExampleClient_sdiff() {
285 ctx := context.Background()
286
287 rdb := redis.NewClient(&redis.Options{
288 Addr: "localhost:6379",
289 Password: "",
290 DB: 0,
291 })
292
293
294
295 rdb.FlushDB(ctx)
296 rdb.Del(ctx, "bikes:racing:france")
297 rdb.Del(ctx, "bikes:racing:usa")
298
299
300
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
316 sort.Strings(res13)
317
318 fmt.Println(res13)
319
320
321
322
323 }
324
325 func ExampleClient_multisets() {
326 ctx := context.Background()
327
328 rdb := redis.NewClient(&redis.Options{
329 Addr: "localhost:6379",
330 Password: "",
331 DB: 0,
332 })
333
334
335
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
341
342
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)
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
376 sort.Strings(res15)
377
378 fmt.Println(res15)
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)
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
403 sort.Strings(res18)
404
405 fmt.Println(res18)
406
407
408
409
410
411
412
413
414 }
415
416 func ExampleClient_srem() {
417 ctx := context.Background()
418
419 rdb := redis.NewClient(&redis.Options{
420 Addr: "localhost:6379",
421 Password: "",
422 DB: 0,
423 })
424
425
426
427 rdb.FlushDB(ctx)
428 rdb.Del(ctx, "bikes:racing:france")
429
430
431
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)
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)
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)
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)
469
470
471
472
473 }
474
View as plain text