Use the Geocoder interface for multiple services
masterThe geo-golang library provides a unified Geocoder interface that allows you to interact with various geocoding providers (like Google Maps, Mapbox, OpenStreetMap, etc.) using a consistent API.
Each provider is implemented as a client that satisfies the geo.Geocoder interface. This allows you to switch between services by changing a single line of code or to use a chained geocoder to implement fallback logic (e.g., if the first service fails, try the second).
To use a service, call its specific constructor (e.g., google.Geocoder(apiKey)) which returns a geo.Geocoder instance. You can then call .Geocode(address) to get coordinates or .ReverseGeocode(lat, lng) to get an address.
// Example of using a specific provider
location, _ := google.Geocoder(apiKey).Geocode("Melbourne VIC")
// Example of using a chained geocoder for fallback
geocoder := chained.Geocoder(
openstreetmap.Geocoder(),
google.Geocoder(apiKey),
)
location, _ := geocoder.Geocode("Melbourne VIC")