Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Tutorial Using Anonymous Structs in Go filter_list
Author
Message
Using Anonymous Structs in Go #1
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!
(This post was last modified: 02-09-2017, 07:59 AM by Hoss.)
this forum is dead

[+] 1 user Likes Hoss's post
Reply

RE: Using Anonymous Structs in Go #2
That's interesting... So what exactly would creating an anonymous struct accomplish when dealing with server sent JSON repsonse(s)?
Scientia potentia est

[Image: inkexplosion.jpg]

Reply

RE: Using Anonymous Structs in Go #3
(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"`
}
(This post was last modified: 02-08-2017, 07:37 PM by Hoss.)
this forum is dead

Reply

RE: Using Anonymous Structs in Go #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]

Reply







Users browsing this thread: 1 Guest(s)