Skip to content

Commit 1da50b3

Browse files
DOC-4213 string code examples (#3102)
Co-authored-by: ofekshenawa <[email protected]>
1 parent f994478 commit 1da50b3

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

doctests/string_example_test.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// EXAMPLE: set_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+
func ExampleClient_set_get() {
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+
// REMOVE_START
23+
rdb.Del(ctx, "bike:1")
24+
// REMOVE_END
25+
26+
// STEP_START set_get
27+
res1, err := rdb.Set(ctx, "bike:1", "Deimos", 0).Result()
28+
29+
if err != nil {
30+
panic(err)
31+
}
32+
33+
fmt.Println(res1) // >>> OK
34+
35+
res2, err := rdb.Get(ctx, "bike:1").Result()
36+
37+
if err != nil {
38+
panic(err)
39+
}
40+
41+
fmt.Println(res2) // >>> Deimos
42+
// STEP_END
43+
44+
// Output:
45+
// OK
46+
// Deimos
47+
}
48+
49+
func ExampleClient_setnx_xx() {
50+
ctx := context.Background()
51+
52+
rdb := redis.NewClient(&redis.Options{
53+
Addr: "localhost:6379",
54+
Password: "", // no password docs
55+
DB: 0, // use default DB
56+
})
57+
58+
// REMOVE_START
59+
rdb.Set(ctx, "bike:1", "Deimos", 0)
60+
// REMOVE_END
61+
62+
// STEP_START setnx_xx
63+
res3, err := rdb.SetNX(ctx, "bike:1", "bike", 0).Result()
64+
65+
if err != nil {
66+
panic(err)
67+
}
68+
69+
fmt.Println(res3) // >>> false
70+
71+
res4, err := rdb.Get(ctx, "bike:1").Result()
72+
73+
if err != nil {
74+
panic(err)
75+
}
76+
77+
fmt.Println(res4) // >>> Deimos
78+
79+
res5, err := rdb.SetXX(ctx, "bike:1", "bike", 0).Result()
80+
81+
if err != nil {
82+
panic(err)
83+
}
84+
85+
fmt.Println(res5) // >>> OK
86+
// STEP_END
87+
88+
// Output:
89+
// false
90+
// Deimos
91+
// true
92+
}
93+
94+
func ExampleClient_mset() {
95+
ctx := context.Background()
96+
97+
rdb := redis.NewClient(&redis.Options{
98+
Addr: "localhost:6379",
99+
Password: "", // no password docs
100+
DB: 0, // use default DB
101+
})
102+
103+
// REMOVE_START
104+
rdb.Del(ctx, "bike:1", "bike:2", "bike:3")
105+
// REMOVE_END
106+
107+
// STEP_START mset
108+
res6, err := rdb.MSet(ctx, "bike:1", "Deimos", "bike:2", "Ares", "bike:3", "Vanth").Result()
109+
110+
if err != nil {
111+
panic(err)
112+
}
113+
114+
fmt.Println(res6) // >>> OK
115+
116+
res7, err := rdb.MGet(ctx, "bike:1", "bike:2", "bike:3").Result()
117+
118+
if err != nil {
119+
panic(err)
120+
}
121+
122+
fmt.Println(res7) // >>> [Deimos Ares Vanth]
123+
// STEP_END
124+
125+
// Output:
126+
// OK
127+
// [Deimos Ares Vanth]
128+
}
129+
130+
func ExampleClient_incr() {
131+
ctx := context.Background()
132+
133+
rdb := redis.NewClient(&redis.Options{
134+
Addr: "localhost:6379",
135+
Password: "", // no password docs
136+
DB: 0, // use default DB
137+
})
138+
139+
// REMOVE_START
140+
rdb.Del(ctx, "total_crashes")
141+
// REMOVE_END
142+
143+
// STEP_START incr
144+
res8, err := rdb.Set(ctx, "total_crashes", "0", 0).Result()
145+
146+
if err != nil {
147+
panic(err)
148+
}
149+
150+
fmt.Println(res8) // >>> OK
151+
152+
res9, err := rdb.Incr(ctx, "total_crashes").Result()
153+
154+
if err != nil {
155+
panic(err)
156+
}
157+
158+
fmt.Println(res9) // >>> 1
159+
160+
res10, err := rdb.IncrBy(ctx, "total_crashes", 10).Result()
161+
162+
if err != nil {
163+
panic(err)
164+
}
165+
166+
fmt.Println(res10) // >>> 11
167+
// STEP_END
168+
169+
// Output:
170+
// OK
171+
// 1
172+
// 11
173+
}

0 commit comments

Comments
 (0)