Geolocation and Maps in Go (Golang): Libraries, Tools, and Code Examples
Developing applications that work with geographic data is becoming increasingly in demand. The Go language (Golang) offers excellent capabilities for creating high-performance geolocation services — from simple GPS trackers to complex mapping services. In this article, we will review the key libraries and tools for working with maps, coordinates, and geodata in Go.
Main Areas of Working with Geodata in Go
Before moving on to specific libraries, it is important to understand what tasks Go developers typically solve in the field of geolocation:
- Geocoding and Reverse Geocoding — converting an address to coordinates and vice versa.
- Distance and Area Calculation — haversine formula, Vincenty, working with ellipsoids.
- Working with GPS Data — parsing NMEA strings, working with tracks.
- Map Visualization — generating static and interactive maps (often via integration with JavaScript).
- Storing and Indexing Geodata — working with PostGIS, MongoDB GeoJSON, Tile38.
Libraries for Geocoding and Geo-Computations
1. go-geo — Basic Geometry and Calculations
One of the simplest and most popular libraries for working with geographic points and lines. It allows calculating the distance between two points using the haversine formula.
package main
import ( "fmt" "github.com/paulmach/go.geo")
func main() { // Create two points (latitude, longitude) point1 := geo.NewPoint(55.751244, 37.618423) // Moscow point2 := geo.NewPoint(59.934280, 30.335099) // Saint Petersburg
// Calculate distance in kilometers distance := point1.GeoDistance(point2) fmt.Printf("Distance between Moscow and SPb: %.2f km", distance)}The library also supports working with polylines, polygons, and geometry simplification (Douglas-Peucker algorithm).
2. geocoding — Integration with Mapping APIs
The github.com/kelvins/geocoding library provides a simple interface for working with Google Maps Geocoding API, OpenStreetMap Nominatim, and other services.
package main
import ( "fmt" "github.com/kelvins/geocoding" "github.com/kelvins/geocoding/places")
func main() { // Use Nominatim (OpenStreetMap) geocoder := geocoding.NewNominatimGeocoder("MyApp/1.0") // Geocoding: address -> coordinates results, err := geocoder.Geocode("Moscow, Red Square", 1) if err != nil { panic(err) } if len(results) > 0 { fmt.Printf("Coordinates: %.4f, %.4f", results[0].Latitude, results[0].Longitude) }
// Reverse geocoding: coordinates -> address addr, err := geocoder.ReverseGeocode(55.751244, 37.618423) if err == nil { fmt.Printf("Address: %s", addr.FormattedAddress) }}3. go-proj — Coordinate System Transformation
For professional work with maps, reprojection from one coordinate system to another (e.g., from WGS84 to Web Mercator) is often required. The github.com/pebbe/go-proj library is a wrapper around PROJ — a powerful library for working with geodetic projections.
Working with GPS Tracks and NMEA
gpsd Library and NMEA Parsing
For reading data from GPS receivers in Go, there are several solutions. One of the most popular is github.com/stratoberry/go-gpsd, which connects to the GPSd daemon. For direct parsing of NMEA strings, use github.com/adrianmo/go-nmea.
package main
import ( "fmt" "github.com/adrianmo/go-nmea") Also in library
Working with ZIP Archives in Java: A Complete Guide to java.util.zip
Caching in Node.js: Redis, Memcached, and Built-in Memory
Working with Kafka in TypeScript: KafkaJS Library and Examples
Dependency Injection Container for Swift (iOS/macOS): Swinject
GraphQL for C# (.NET): Complete Guide to the GraphQL.NET Library