-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathgeo_tutorial_test.go
143 lines (114 loc) · 2.45 KB
/
geo_tutorial_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// EXAMPLE: geo_tutorial
// HIDE_START
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
// HIDE_END
func ExampleClient_geoadd() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
// make sure we are working with fresh database
rdb.FlushDB(ctx)
rdb.Del(ctx, "bikes:rentable")
// REMOVE_END
// STEP_START geoadd
res1, err := rdb.GeoAdd(ctx, "bikes:rentable",
&redis.GeoLocation{
Longitude: -122.27652,
Latitude: 37.805186,
Name: "station:1",
}).Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 1
res2, err := rdb.GeoAdd(ctx, "bikes:rentable",
&redis.GeoLocation{
Longitude: -122.2674626,
Latitude: 37.8062344,
Name: "station:2",
}).Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> 1
res3, err := rdb.GeoAdd(ctx, "bikes:rentable",
&redis.GeoLocation{
Longitude: -122.2469854,
Latitude: 37.8104049,
Name: "station:3",
}).Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> 1
// STEP_END
// Output:
// 1
// 1
// 1
}
func ExampleClient_geosearch() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
// start with fresh database
rdb.FlushDB(ctx)
rdb.Del(ctx, "bikes:rentable")
_, err := rdb.GeoAdd(ctx, "bikes:rentable",
&redis.GeoLocation{
Longitude: -122.27652,
Latitude: 37.805186,
Name: "station:1",
}).Result()
if err != nil {
panic(err)
}
_, err = rdb.GeoAdd(ctx, "bikes:rentable",
&redis.GeoLocation{
Longitude: -122.2674626,
Latitude: 37.8062344,
Name: "station:2",
}).Result()
if err != nil {
panic(err)
}
_, err = rdb.GeoAdd(ctx, "bikes:rentable",
&redis.GeoLocation{
Longitude: -122.2469854,
Latitude: 37.8104049,
Name: "station:3",
}).Result()
if err != nil {
panic(err)
}
// REMOVE_END
// STEP_START geosearch
res4, err := rdb.GeoSearch(ctx, "bikes:rentable",
&redis.GeoSearchQuery{
Longitude: -122.27652,
Latitude: 37.805186,
Radius: 5,
RadiusUnit: "km",
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> [station:1 station:2 station:3]
// STEP_END
// Output:
// [station:1 station:2 station:3]
}