GO语言操作redis对geo类型进行curd
时间: 2023-08-20 19:05:17 浏览: 71
在 Go 语言中对 Redis 的 Geo 数据类型进行 CRUD 操作,可以使用 go-redis 库提供的相应函数。下面是一个示例代码,演示了如何使用 Go 语言操作 Redis 的 Geo 数据类型:
```go
package main
import (
"fmt"
"github.com/go-redis/redis"
)
func main() {
// 创建 Redis 客户端
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // Redis 无密码连接
DB: 0, // 默认数据库
})
// 测试连接
pong, err := client.Ping().Result()
fmt.Println(pong, err)
// 添加地理位置数据
err = client.GeoAdd("mylocations", &redis.GeoLocation{
Name: "Beijing",
Longitude: 116.407526,
Latitude: 39.90403,
}).Err()
if err != nil {
panic(err)
}
// 获取地理位置数据
locations, err := client.GeoRadius("mylocations", 116.407526, 39.90403, &redis.GeoRadiusQuery{
Radius: 100, // 100 千米内的地理位置
Unit: "km",
WithCoord: true,
WithDist: true,
WithGeoHash: true,
}).Result()
if err != nil {
panic(err)
}
fmt.Println(locations)
// 删除地理位置数据
err = client.ZRem("mylocations", "Beijing").Err()
if err != nil {
panic(err)
}
}
```
这个示例代码演示了如何使用 Go 语言操作 Redis 的 Geo 数据类型。其中,我们使用 go-redis 库提供的 Geo 相关函数,包括 GeoAdd、GeoRadius、ZRem 等。GeoAdd 函数用于向 Geo 数据类型中添加地理位置数据,GeoRadius 函数用于查询指定地理位置范围内的数据,ZRem 函数用于删除指定的地理位置数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)