Sinisterly
Tutorial Using Anonymous Structs in Go - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Java, JVM, & JRE (https://sinister.ly/Forum-Java-JVM-JRE)
+--- Thread: Tutorial Using Anonymous Structs in Go (/Thread-Tutorial-Using-Anonymous-Structs-in-Go)



Using Anonymous Structs in Go - Hoss - 02-08-2017

This is just a short tutorial about using anonymous structs in Go, which can be very handy when dealing with JSON server responses (especially from Facebook).

So say you have this JSON response from the server:

Code:
{  "lights" : {    {"livingroomLight" : true},    {"bathroomLight" : false},    {"kitchenLight" : false}  } }

and you want to put `lights` into a Go struct that looks like this:

Code:
type Lights struct {  Toggles map[string]bool }

with anonymous structs you can now do this with ease!

Code:
type Lights struct {  Toggles map[string]bool } var lights Lights if err := json.Unmarshal("json_data", &struct{  Temp *map[string]bool `json:"lights"` }{  Temp: &lights.Toggles, }); err != nil {  // TODO: handle error }

Here is a link to a Go playground where I implemented this example.

Good luck with your Go coding!


RE: Using Anonymous Structs in Go - DarkMuse - 02-08-2017

That's interesting... So what exactly would creating an anonymous struct accomplish when dealing with server sent JSON repsonse(s)?


RE: Using Anonymous Structs in Go - Hoss - 02-08-2017

(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"` }



RE: Using Anonymous Structs in Go - DarkMuse - 02-08-2017

(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.