Skip to content

Instantly share code, notes, and snippets.

@pote
Last active February 4, 2018 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pote/20073725cbedbb413dfe860be04fe48c to your computer and use it in GitHub Desktop.
Save pote/20073725cbedbb413dfe860be04fe48c to your computer and use it in GitHub Desktop.
// So, in order to unmarshal json easily with Go it's important to code Go structs that
// follow the structure of the data you will receive. It might be a bit of extra coding
// up front, but I think it has some nice benefits since you can then can work with native
// structs and have type safety enforced.
// Keep in mind that this is an example so my naming might not be the best, but I hope it
// will be helpful to you anyways.
// I created the structs and their relationships based on the sample response from the API
// documentation you sent me, which is copied at the bottom for clarity.
// Feel free to reach out if any of this is confusing or I can help with anything else!
type MapsQuery struct {
Status string `json:"status"`
ErrorMessage string `json:"error_message"`
Results []Result `json:"results"`
}
type Result struct {
PlaceID string `json:"place_id"`
FormattedAddress string `json:"formatted_address"`
Types []string `json:"types"`
AddressComponents []AddressComponent `json:"address_components"`
Geometry Geometry `json:"geometry"`
}
type AddressComponent struct {
LongName string `json:"long_name"`
ShortName string `json:"short_name"`
Types []string `json:"types"`
}
type Geometry struct {
LocationType string `json:"location_type"`
Location Location `json:"location"`
Viewport map[string]Location `json:"viewport"`
}
type Location struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
}
// This way, you can instanciate a maps query and unmarshal the response into it.
var queryResult MapsQuery
json.Unmarshal(yourResponseBody, queryResult)
// And then you have a Go struct filled up with everything in it, so you can manipulate it like this:
queryResult.Status //=> "OK"
queryResult.Results[0].PlaceID //=> "ChIJ2eUgeAK6j4ARbn5u_wAGqWA"
queryResult.Results[0].AddressComponents[0].LongName //=> "1600"
queryResult.Results[0].Geometry.Location.Lat //=> 37.4224764
queryResult.Results[0].Geometry.Viewport["northeast"].Lat //=> 37.4238253802915
// You can iterate throught the results as you would in normal Go code.
for _, result in queryResult.Results {
// ...
}
// I made this example with the following sample response in mind, from the documentation:
`
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Pkwy",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4224764,
"lng" : -122.0842499
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4238253802915,
"lng" : -122.0829009197085
},
"southwest" : {
"lat" : 37.4211274197085,
"lng" : -122.0855988802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
`
@izgeri
Copy link

izgeri commented Feb 4, 2018

This was very useful, thanks! One thing that came up is that json.Unmarshal requires a pointer for its second argument, so I had to modify line 47 to json.Unmarshal(yourResponseBody, &queryResult) - then it worked fine.

In the end I'm not sure I would choose Golang to write this particular bit of code in again, but it was a fun exercise :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment