Geolocation and Maps in Go (Golang): Library Overview and Code Examples
Go (Golang) is a modern compiled language that is gaining popularity in backend development due to its performance, simplicity, and built-in concurrency support. When creating educational, logistics, tourism, and other web services, it is often necessary to work with geographic data: calculating distances between points, checking if coordinates fall within zones, building routes, and displaying maps. In this article, we will review the main Go libraries for working with geolocation and maps, and provide practical code examples.
Main Libraries for Working with Geolocation
1. github.com/paulmach/orb — Universal Library for Working with Geometric Objects
The orb library provides types and functions for working with points, lines, polygons, and other geometric primitives. It supports GeoJSON, boolean geometry operations, projections, and coordinate transformations. This is a foundational library upon which many other solutions are built.
package main
import ( "fmt" "github.com/paulmach/orb" "github.com/paulmach/orb/geo")
func main() { // Define two points (latitude, longitude) moscow := orb.Point{37.6173, 55.7558} spb := orb.Point{30.3351, 60.0000}
// Calculate distance in kilometers (haversine formula) distance := geo.Distance(moscow, spb) fmt.Printf("Distance from Moscow to Saint Petersburg: %.2f km", distance)}2. github.com/twpayne/go-geom — Powerful Library for Geometric Operations
This library offers lower-level access to geometry and supports many OGC (Open Geospatial Consortium) standards. It is ideal for complex calculations: polygon intersections, buffering, projection transformations.
import ( "github.com/twpayne/go-geom" "github.com/twpayne/go-geom/encoding/geojson")
// Example: creating a point and serializing to GeoJSONpoint := geom.NewPoint(geom.XY).SetCoords(geom.Coord{37.6173, 55.7558})data, _ := geojson.Marshal(point)fmt.Println(string(data))// Output: {"type":"Point","coordinates":[37.6173,55.7558]}3. github.com/kellydunn/golang-geo — Simple Library for Distance Calculation and Geocoding
The golang-geo library provides a simple API for working with points, calculating distances (in miles and kilometers), and geocoding via the Google Maps API or OpenStreetMap (Nominatim).
import "github.com/kellydunn/golang-geo"
func main() { p1 := geo.NewPoint(55.7558, 37.6173) p2 := geo.NewPoint(60.0000, 30.3351) dist := p1.GreatCircleDistance(p2) // in kilometers fmt.Printf("Distance: %.2f km", dist)}Libraries for Working with Maps and Visualization
4. github.com/umahmood/leaflet-go — Integration with Leaflet.js for Go
Leaflet is one of the most popular JavaScript libraries for interactive maps. leaflet-go allows you to generate HTML code with maps directly from a Go application. This is convenient for educational sites and simple web services.
import "github.com/umahmood/leaflet-go"
func main() { // Create a map centered on Moscow m := leaflet.NewMap( leaflet.Center(55.7558, 37.6173), leaflet.Zoom(10), leaflet.TileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"), )
// Add a marker m.AddMarker(leaflet.Marker( leaflet.LatLng(55.7558, 37.6173), leaflet.Popup("Moscow, Kremlin"), ))
// Output the map HTML code html, _ := m.Render() fmt.Println(html)}5. github.com/ringsaturn/leaflet — Alternative Wrapper for Leaflet
This library
Also in library
JWT tokens in Rust: jsonwebtoken library and authentication
Testcontainers .NET for C#: Complete Guide with Examples
Testing in JavaScript (Node.js): Frameworks and Tools
Task Queues in Go: Overview of async, gocelery, and machinery Libraries