All tutorials
Golang
The what3words Free API plan will no longer include convert-to-coordinate requests. To continue using this feature, please upgrade to one of our paid plans starting from £7.99/month. Learn more about our API plans here.
As a reminder, emergency services can make use of the what3words API for free, and we have a dedicated plan for NGOs and registered charities. If your organisation falls within these categories, please get in touch. For any questions or assistance, feel free to reach out to support@what3words.com.
The Go wrapper is useful for Go developers who wish to seamlessly integrate the what3words Public API into their Go applications, without the hassle of having to manage the low level API calls themselves.
A full example of Go application is available in our GitHub repository.
First, install the Go wrapper by running:
go get github.com/what3words/w3w-go-wrapper
Import the Package
Start by importing the what3words package in your Go code:
import ( "github.com/what3words/w3w-go-wrapper/pkg/apis/v3" what3words "github.com/what3words/w3w-go-wrapper" )
Initialise the Geocoder service
Create a new geocoder instance using your what3words API key. By default, the wrapper will point to the public version of the what3words API.
Public API:
apiKey := "<YOUR_API_KEY>" svc := what3words.NewService(apiKey)
However, if required, the wrapper can be configured to point to a custom endpoint, for example a locally running version of the API. In this case an optional WithCustomBaseURL
parameter can be passed to the what3words.NewService
constructor.
API Server:
svc := what3words.NewService(apiKey, WithCustomBaseURL("http://localhost:8080"))
Convert to what3words address
This function converts coordinates (expressed as latitude and longitude) to a what3words address.
More information about ConvertTo3wa
, including returned results is available in the what3words REST API documentation.
Find the words for (51.520847,-0.195521
):
func main() { apiKey := "<YOUR_API_KEY>" svc := w3w.NewService(apiKey) resp, err := svc.V3.ConvertTo3wa(context.Background(), v3.Coordinates{ Lat: 51.520847, Lng: -0.195521, }, nil) if err != nil { panic(err) } fmt.Println(resp.Json.Words) }
Convert to coordinates
This function converts a what3words address to a position, expressed as coordinates of latitude and longitude.
It takes the words parameter as a string of a what3words 'table.book.chair'
More information about ConvertToCoordinates
, including returned results is available in the what3words REST API documentation.
Find the words for ///filled.count.soap
:
func main() { apiKey := "<YOUR_API_KEY>" svc := w3w.NewService(apiKey) resp, err := svc.V3.ConvertToCoordinates(context.Background(), "filled.count.soap", nil) if err != nil { panic(err) } // By default response JSON is used fmt.Println(resp.Json.Coordinates) // Getting a geojson response geoResp, err := svc.V3.ConvertToCoordinates(context.Background(), "filled.count.soap", &v3.ConvertAPIOpts{ Format: v3.ResponseFormatGeoJson, }) if err != nil { panic(err) } // If format GeoJson is not set in options, GeoJson attribute of the response will be set to nil fmt.Println(geoResp.GeoJson.Features[0].Geometry.Coordinates) }
AutoSuggest
When presented with a what3words address which may be incorrectly entered, AutoSuggest returns a list of potential correct 3 word addresses. It needs the first two words plus at least the first character of the third word to produce suggestions.
This method provides corrections for mis-typed words (including plural VS singular), and words being in the wrong order.
Optionally, clipping can narrow down the possibilities, and limit results to:
- One or more countries
- A geographic area (a circle, box or polygon)
This dramatically improves results, so we recommend that you use clipping if possible.
To improve results even further, set the Focus
to user’s current location. This will make AutoSuggest
return results which are closer to the user.
More information about AutoSuggest
, including returned results is available in the what3words REST API documentation.
Code example Simple basic call:
func main() { apiKey := "<YOUR_API_KEY>" svc := w3w.NewService(apiKey) resp, err := svc.V3.AutoSuggest(context.Background(), "filled.count.so") if err != nil { panic(err) } fmt.Println(resp) }
Code example AutoSuggest, clipping the results returned to France and Germany:
func main() { apiKey := "<YOUR_API_KEY>" svc := w3w.NewService(apiKey) resp, err := svc.V3.AutoSuggest(context.Background(), "filled.count.so",&v3.AutoSuggestOpts{ ClipToCountry: []string{"FR", "DE"}, }) if err != nil { panic(err) } fmt.Println(resp) }
Code example AutoSuggest, Focus on (51.4243877,-0.34745
).
func main() { apiKey := "<YOUR_API_KEY>" svc := w3w.NewService(apiKey) resp, err := svc.V3.AutoSuggest(context.Background(), "filled.count.so",&v3.AutoSuggestOpts{ Focus: &core.Coordinates{ Lat: 51.4243877, Lng: -0.34745, }, }) if err != nil { panic(err) } fmt.Println(resp) }
Grid section
Grid section returns a section of the what3words 3m x 3m grid as a set of horizontal and vertical lines covering the requested area, which can then be drawn onto a map.
The requested box must not exceed 4km from corner to corner, or a BadBoundingBoxTooBig
error will be returned.
More information about GridSection
, including returned results is available in the what3words REST API documentation.
Get a grid for 52.207988,0.116126
) in the south west, and 52.208867,0.117540
in the north east:
func main() { apiKey := "<YOUR_API_KEY>" svc := w3w.NewService(apiKey) resp, err := svc.V3.GridSection(context.Background(), v3.BoundingBox{ SouthWest: v3.Coordinates{ Lat: 52.207988, Lng: 0.116126, }, NorthEast: v3.Coordinates{ Lat: 52.208867, Lng: 0.117540, }, }, nil) if err != nil { panic(err) } fmt.Println(resp.Json.Lines) }
Available languages
This function returns the currently supported languages. It will return the two letter code, and the name of the language both in that language and in English.
More information about AvailableLanguages
, including returned results is available in the what3words REST API documentation.
func main() { apiKey := "<YOUR_API_KEY>" svc := w3w.NewService(apiKey) resp, err := svc.V3.AvailableLanguages(context.Background()) if err != nil { panic(err) } fmt.Println(resp) }
If you encounter errors or issues related to convert-to-coordinate requests while using the Free plan, please check the network panel for the following error message Error 402 payment required
and its response, indicating the need to upgrade to a higher plan:
{ "error": { "code": "QuotaExceeded", "message": "Quota Exceeded. Please upgrade your usage plan, or contact support@what3words.com" } }
For more information, visit our API plans page. If you need further assistance, contact support@what3words.com.