RE: Using Anonymous Structs in Go 02-08-2017, 07:38 PM
#4
(02-08-2017, 07:33 PM)Hoss Wrote:(02-08-2017, 07:28 PM)DarkMuse Wrote: That's interesting... So what exactly would creating an anonymous struct accomplish when dealing with server sent JSON repsonse(s)?
because you don't always want the outer JSON object, you just want the data portion. To do this without an anonymous struct you would have to define a new type like:
Code:type TempData struct {
Lights map[string]bool
}
and then create an instance of that type and after you populate it with the response you have to extract the Lights map.
Facebook responses look like this:
Code:{
"data": [
{
"name": "Girl Rising",
"id": "118832528170370"
},
{
"name": "Global Citizen",
"id": "289446947817747"
}
],
"paging": {
"cursors": {
"before": "MTE4ODMyNTI4MTcwMzcw",
"after": "Mjg5NDQ2OTQ3ODE3NzQ3"
},
"next": "https://graph.facebook.com/v2.8/191187460289/likes?access_token=EAACEdEose0cBAIzwGrDLZCzWNx09qvfWrwVKRRfYYGIPeofDo8awqjZBGsb16kZAHjOZCvrhKCP6MyQMmISEBqiAFhqKDI3u0CwUZB08B3TmCu0SXZB2nZBlQ64kHCK6IotZAykTZARZBVA7b9F5EZAwnRivq4C2xIvDVuvoLhZAdBzZBr8So6i0VrW2Bxj37qTvbRdsZD&pretty=0&limit=2&after=Mjg5NDQ2OTQ3ODE3NzQ3"
}
}
and so I would make an anonymous struct to extract the data objects from the data tag, maybe like:
Code:struct {
*Data []struct {
Name string `json:"name"`
ID string `json:"id"`
} `json:"data"`
}
That's fucking dope dude. You're a legend.
Scientia potentia est
![[Image: inkexplosion.jpg]](http://i0.wp.com/techverse.net/wp-content/uploads/2013/09/inkexplosion.jpg)