Go Redis Geo
In this tutorial, you'll learn about Go Redis: Geo Commands. We cover key concepts, practical examples, and best practices.
Redis geo commands -- Use GEOADD, GEORADIUS, GEODIST for location-based queries.
The Problem
Redis geo commands take (longitude, latitude) order, not (latitude, longitude). Using wrong order causes positions to be off by thousands of kilometers.
Wrong
rdb.GeoAdd(ctx, "locations", &redis.GeoLocation{
Name: "Tokyo",
Latitude: 35.6762, Longitude: 139.6503, // Correct order
}).Err()
res, _ := rdb.GeoRadius(ctx, "locations", 139, 35, &redis.GeoRadiusQuery{
Radius: 100, Unit: "km",
}).Result()
Output:
// GeoRadius with lon=139, lat=35 finds nearby locations
Right
res, _ := rdb.GeoRadius(ctx, "locations", 139, 35,
&redis.GeoRadiusQuery{Radius: 100, Unit: "km", WithDist: true}).Result()
for _, loc := range res {
fmt.Printf("%s: %.2f km
", loc.Name, loc.Dist)
}
Output:
Tokyo: 0.52 km
Prevention
- Geo commands use (lon, lat) order
- Use GeoRadius for nearby queries
- Use GeoDist for distance between two points
- Use WithCoord, WithDist, WithHash for extra info
- Radius in meters (default) or km/mi/ft
Common Mistakes with redis geo
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world GO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. DodaTech tutorials help Go developers build production-ready software used by millions.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro