I am pretty new to Go, learning how to use it. Wanted to test hitting the Google Places API, but am having some trouble in writing the request. It seems the request goes through, I receive something in the body, but I cannot Unmarshall it. I just want to see the json printed in string form so I can try to decode it. Any help is appreciated, thank you!
type place struct {
Name string `json:candidates`
}
func main() {
places("Grill")
}
func places(inputText string) {
url := "https://maps.googleapis.com/maps/api/place/findplacefromtext/"
placesClient := http.Client{
Timeout: time.Second * 10,
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("User-Agent", "Testing how to query API's from parameters")
q := req.URL.Query()
q.Add("key", PLACES_KEY)
q.Add("input", inputText)
q.Add("inputtype", "textquery")
req.URL.RawQuery = q.Encode()
pln(req.URL.String())
res, getErr := placesClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
output := place{}
jsonErr := json.Unmarshal(body, &output)
if jsonErr != nil {
log.Fatal(jsonErr)
}
pln(output)
}