Geolocation and Maps in Go (Golang): A Complete Overview of Tools
Go (Golang) is a modern compiled language with excellent performance and simplicity of concurrent programming. When developing server applications, monitoring systems, logistics platforms, or geoinformation services, the need to work with geodata often arises: determining user location, building routes, displaying objects on a map. In this article, we will review the key libraries and approaches for working with geolocation and maps in Go.
1. Main Libraries for Working with Geodata in Go
The Go ecosystem offers several powerful libraries for geospatial computations. Let's look at the most popular ones:
go-geo— a library for working with geometric types (points, lines, polygons). Supports latitude/longitude, distance, area, buffer zones.go-spatial/geom— an implementation of the OGC (Open Geospatial Consortium) standard. Allows working with geometry and performing spatial queries.paulmach/orb— a fast and clean library for geospatial operations. Includes points, lines, polygons, as well as algorithms for working with GeoJSON and MVT (Mapbox Vector Tiles).golang-geo— a simple library for calculating distances using the haversine formula and determining nearest points.
2. Determining Location (Geolocation) in Go
To obtain device coordinates (IP address, GPS) in Go, the following approaches are used:
- IP-based determination — the
ip2location-golibrary or services likeipapi.co,ipstack. Example of a request to an external API:
package main
import ( "encoding/json" "fmt" "net/http")
type GeoIP struct { Lat float64 `json:"latitude"` Lon float64 `json:"longitude"`}
func main() { resp, err := http.Get("http://ip-api.com/json/") if err != nil { panic(err) } defer resp.Body.Close() var geo GeoIP json.NewDecoder(resp.Body).Decode(&geo) fmt.Printf("Coordinates: %.4f, %.4f", geo.Lat, geo.Lon)}- GPS coordinates from a mobile device — usually transmitted via REST API or WebSocket. The Go server receives JSON with
latandlonfields. - Using the
golang-geolibrary to calculate distance:
package main
import ( "fmt" "github.com/kellydunn/golang-geo")
func main() { p1 := geo.NewPoint(55.7558, 37.6173) // Moscow p2 := geo.NewPoint(59.9343, 30.3351) // Saint Petersburg dist := p1.GreatCircleDistance(p2) // in kilometers fmt.Printf("Distance: %.2f km", dist)}3. Working with Maps: OpenStreetMap and Leaflet
To display maps on a web page generated by a Go backend, the Go + Leaflet.js combination is most often used. Leaflet is a lightweight JavaScript library for interactive maps. Go is responsible for generating the HTML template with coordinates.
3.1. Basic Map with Leaflet and Go
Let's create a simple HTTP server that returns a page with a map:
package main
import ( "html/template" "net/http")
type Location struct { Lat float64 Lon float64}
const tpl = `<!DOCTYPE html><html><head> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" /> <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script></head><body> <div id="map" style="height: 500px;"></div> <script> var map = L.map('map').setView([{{.Lat}},