Home / Go integration guide
Go integration guide
Call the LatLongAPI from Go with a JSON POST request, server-side license configuration, and HTTP error handling.
Configure your application
This Go example uses the standard library. Save it as main.go. The request and response structs are declared at package level and reused inside main.
In a Bash or compatible shell, set LATLONG_API_BASE_URL to your assigned HTTPS origin and LATLONG_API_LICENSE to your license. For testing, use the test origin below with a test license. Replace YOUR_TEST_LICENSE with the license issued for that environment. Keep the license outside source control and browser code.
export LATLONG_API_BASE_URL="https://v1-dev.latlongapi.com"
export LATLONG_API_LICENSE="YOUR_TEST_LICENSE"Request an address
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"time"
)
type ReverseRequest struct {
Latitude float64 `json:"lat"`
Longitude float64 `json:"long"`
License string `json:"api"`
}
type ReverseResult struct {
ShortAddress string `json:"short_address"`
}
type ReverseResponse struct {
Result ReverseResult `json:"result"`
}
func main() {
base := os.Getenv("LATLONG_API_BASE_URL")
license := os.Getenv("LATLONG_API_LICENSE")
if base == "" || license == "" {
panic("Set LATLONG_API_BASE_URL and LATLONG_API_LICENSE")
}
request := ReverseRequest{
Latitude: 51.5007,
Longitude: -0.1246,
License: license,
}
payload, err := json.Marshal(request)
if err != nil {
panic(err)
}
client := &http.Client{Timeout: 10 * time.Second}
endpoint := strings.TrimRight(base, "/") + "/api/v1/reverse"
response, err := client.Post(endpoint, "application/json", bytes.NewReader(payload))
if err != nil {
panic(err)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
panic(response.Status)
}
var data ReverseResponse
if err := json.NewDecoder(response.Body).Decode(&data); err != nil {
panic(err)
}
fmt.Println(data.Result.ShortAddress)
}
Run the example
go run main.goUse the result
The example prints result.short_address. Read result.long_address for a longer description. Check nullable city and country fields before displaying them.
Handle failures in your service
Set a timeout, catch request errors, and record the status and request identifier without logging the license. Retry temporary failures with bounded backoff. A 401 requires a valid license; a 422 requires correcting the input.