Geocode an Address in Go

Cloudmersive
2 min readApr 5, 2021

Looking for ways to improve your clients’ experience? Understanding where they live with assist you with information on their demographics, which in turn can assist with marketing planning and strategy. By using following API in Go, you will be able to pinpoint the exact latitude and longitude from an address; this will also help with delivery services. The input for the function should include the street address, city, state/province, postal code, and the country name and code.

To use the API, we simply need to input the above information (along with the API key) and we can call the function with the following code:

package mainimport (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/validate/text-input/check/xss"
method := "POST"
payload := strings.NewReader(`"<string>"`)client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

This will return whether the address was valid, as well as the latitude and longitude for its location. You can retrieve the API key from the Cloudmersive website by registering for a free account; this will provide you with 800 monthly calls across our library of APIs.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.