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_geoadd() {
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:rentable")
27
28
29
30 res1, err := rdb.GeoAdd(ctx, "bikes:rentable",
31 &redis.GeoLocation{
32 Longitude: -122.27652,
33 Latitude: 37.805186,
34 Name: "station:1",
35 }).Result()
36
37 if err != nil {
38 panic(err)
39 }
40
41 fmt.Println(res1)
42
43 res2, err := rdb.GeoAdd(ctx, "bikes:rentable",
44 &redis.GeoLocation{
45 Longitude: -122.2674626,
46 Latitude: 37.8062344,
47 Name: "station:2",
48 }).Result()
49
50 if err != nil {
51 panic(err)
52 }
53
54 fmt.Println(res2)
55
56 res3, err := rdb.GeoAdd(ctx, "bikes:rentable",
57 &redis.GeoLocation{
58 Longitude: -122.2469854,
59 Latitude: 37.8104049,
60 Name: "station:3",
61 }).Result()
62
63 if err != nil {
64 panic(err)
65 }
66
67 fmt.Println(res3)
68
69
70
71
72
73
74 }
75
76 func ExampleClient_geosearch() {
77 ctx := context.Background()
78
79 rdb := redis.NewClient(&redis.Options{
80 Addr: "localhost:6379",
81 Password: "",
82 DB: 0,
83 })
84
85
86
87 rdb.FlushDB(ctx)
88 rdb.Del(ctx, "bikes:rentable")
89
90 _, err := rdb.GeoAdd(ctx, "bikes:rentable",
91 &redis.GeoLocation{
92 Longitude: -122.27652,
93 Latitude: 37.805186,
94 Name: "station:1",
95 }).Result()
96
97 if err != nil {
98 panic(err)
99 }
100
101 _, err = rdb.GeoAdd(ctx, "bikes:rentable",
102 &redis.GeoLocation{
103 Longitude: -122.2674626,
104 Latitude: 37.8062344,
105 Name: "station:2",
106 }).Result()
107
108 if err != nil {
109 panic(err)
110 }
111
112 _, err = rdb.GeoAdd(ctx, "bikes:rentable",
113 &redis.GeoLocation{
114 Longitude: -122.2469854,
115 Latitude: 37.8104049,
116 Name: "station:3",
117 }).Result()
118
119 if err != nil {
120 panic(err)
121 }
122
123
124
125 res4, err := rdb.GeoSearch(ctx, "bikes:rentable",
126 &redis.GeoSearchQuery{
127 Longitude: -122.27652,
128 Latitude: 37.805186,
129 Radius: 5,
130 RadiusUnit: "km",
131 },
132 ).Result()
133
134 if err != nil {
135 panic(err)
136 }
137
138 fmt.Println(res4)
139
140
141
142
143 }
144
View as plain text