1
2
3 package example_commands_test
4
5 import (
6 "context"
7 "fmt"
8
9 "github.com/redis/go-redis/v9"
10 )
11
12
13
14 func ExampleClient_set_get_all() {
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, "bike:1")
27
28
29
30 hashFields := []string{
31 "model", "Deimos",
32 "brand", "Ergonom",
33 "type", "Enduro bikes",
34 "price", "4972",
35 }
36
37 res1, err := rdb.HSet(ctx, "bike:1", hashFields).Result()
38
39 if err != nil {
40 panic(err)
41 }
42
43 fmt.Println(res1)
44
45 res2, err := rdb.HGet(ctx, "bike:1", "model").Result()
46
47 if err != nil {
48 panic(err)
49 }
50
51 fmt.Println(res2)
52
53 res3, err := rdb.HGet(ctx, "bike:1", "price").Result()
54
55 if err != nil {
56 panic(err)
57 }
58
59 fmt.Println(res3)
60
61 cmdReturn := rdb.HGetAll(ctx, "bike:1")
62 res4, err := cmdReturn.Result()
63
64 if err != nil {
65 panic(err)
66 }
67
68 fmt.Println(res4)
69
70
71 type BikeInfo struct {
72 Model string `redis:"model"`
73 Brand string `redis:"brand"`
74 Type string `redis:"type"`
75 Price int `redis:"price"`
76 }
77
78 var res4a BikeInfo
79
80 if err := cmdReturn.Scan(&res4a); err != nil {
81 panic(err)
82 }
83
84 fmt.Printf("Model: %v, Brand: %v, Type: %v, Price: $%v\n",
85 res4a.Model, res4a.Brand, res4a.Type, res4a.Price)
86
87
88
89
90
91
92
93
94
95 }
96
97 func ExampleClient_hmget() {
98 ctx := context.Background()
99
100 rdb := redis.NewClient(&redis.Options{
101 Addr: "localhost:6379",
102 Password: "",
103 DB: 0,
104 })
105
106
107
108 rdb.FlushDB(ctx)
109 rdb.Del(ctx, "bike:1")
110
111
112 hashFields := []string{
113 "model", "Deimos",
114 "brand", "Ergonom",
115 "type", "Enduro bikes",
116 "price", "4972",
117 }
118
119 _, err := rdb.HSet(ctx, "bike:1", hashFields).Result()
120
121 if err != nil {
122 panic(err)
123 }
124
125
126 cmdReturn := rdb.HMGet(ctx, "bike:1", "model", "price")
127 res5, err := cmdReturn.Result()
128
129 if err != nil {
130 panic(err)
131 }
132
133 fmt.Println(res5)
134
135 type BikeInfo struct {
136 Model string `redis:"model"`
137 Brand string `redis:"-"`
138 Type string `redis:"-"`
139 Price int `redis:"price"`
140 }
141
142 var res5a BikeInfo
143
144 if err := cmdReturn.Scan(&res5a); err != nil {
145 panic(err)
146 }
147
148 fmt.Printf("Model: %v, Price: $%v\n", res5a.Model, res5a.Price)
149
150
151
152
153
154
155 }
156
157 func ExampleClient_hincrby() {
158 ctx := context.Background()
159
160 rdb := redis.NewClient(&redis.Options{
161 Addr: "localhost:6379",
162 Password: "",
163 DB: 0,
164 })
165
166
167
168 rdb.FlushDB(ctx)
169 rdb.Del(ctx, "bike:1")
170
171
172 hashFields := []string{
173 "model", "Deimos",
174 "brand", "Ergonom",
175 "type", "Enduro bikes",
176 "price", "4972",
177 }
178
179 _, err := rdb.HSet(ctx, "bike:1", hashFields).Result()
180
181 if err != nil {
182 panic(err)
183 }
184
185
186 res6, err := rdb.HIncrBy(ctx, "bike:1", "price", 100).Result()
187
188 if err != nil {
189 panic(err)
190 }
191
192 fmt.Println(res6)
193
194 res7, err := rdb.HIncrBy(ctx, "bike:1", "price", -100).Result()
195
196 if err != nil {
197 panic(err)
198 }
199
200 fmt.Println(res7)
201
202
203
204
205
206 }
207
208 func ExampleClient_incrby_get_mget() {
209 ctx := context.Background()
210
211 rdb := redis.NewClient(&redis.Options{
212 Addr: "localhost:6379",
213 Password: "",
214 DB: 0,
215 })
216
217
218
219 rdb.FlushDB(ctx)
220 rdb.Del(ctx, "bike:1:stats")
221
222
223
224 res8, err := rdb.HIncrBy(ctx, "bike:1:stats", "rides", 1).Result()
225
226 if err != nil {
227 panic(err)
228 }
229
230 fmt.Println(res8)
231
232 res9, err := rdb.HIncrBy(ctx, "bike:1:stats", "rides", 1).Result()
233
234 if err != nil {
235 panic(err)
236 }
237
238 fmt.Println(res9)
239
240 res10, err := rdb.HIncrBy(ctx, "bike:1:stats", "rides", 1).Result()
241
242 if err != nil {
243 panic(err)
244 }
245
246 fmt.Println(res10)
247
248 res11, err := rdb.HIncrBy(ctx, "bike:1:stats", "crashes", 1).Result()
249
250 if err != nil {
251 panic(err)
252 }
253
254 fmt.Println(res11)
255
256 res12, err := rdb.HIncrBy(ctx, "bike:1:stats", "owners", 1).Result()
257
258 if err != nil {
259 panic(err)
260 }
261
262 fmt.Println(res12)
263
264 res13, err := rdb.HGet(ctx, "bike:1:stats", "rides").Result()
265
266 if err != nil {
267 panic(err)
268 }
269
270 fmt.Println(res13)
271
272 res14, err := rdb.HMGet(ctx, "bike:1:stats", "crashes", "owners").Result()
273
274 if err != nil {
275 panic(err)
276 }
277
278 fmt.Println(res14)
279
280
281
282
283
284
285
286
287
288
289 }
290
View as plain text