All tutorials

Golang

intermediate

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.

1
2Installation

First, install the Go wrapper by running:

go get github.com/what3words/w3w-go-wrapper
Copied
3Setup

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"
)
Copied

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)
Copied

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"))
Copied
4Usage

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)
}
Copied

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)
}
Copied

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)
}
Copied

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)
}
Copied

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)
}
Copied

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)
}
Copied

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)
}
Copied
Server and ScriptingAdd a 3 word address input fieldBatch convert 3 word addresses or co-ordinatesDetect if text looks like a 3 word addressDisplay a 3 word addressUse 3 word addresses within an address searchGo

Related tutorials