Given the following JSON responses:
GET /things
{
"things": [
{
"name": "spoon"
},
{
"name": "fork"
},
{
"name": "knife"
}
]
}
GET /things/spoon
{
"thing": {
"name": "spoon"
}
}
What's the best way to represent these two responses in Go? I was thinking of using three structs: a root struct for multiple things, a root struct for one thing, and struct for a singular thing.
type RootThings struct {
Things []Thing `json:things`
}
type RootThing struct {
Thing *Thing `json:thing`
}
type Thing struct {
Name string `json:name`
}
Something about this feels dirty though :(
I would combine singular things and multiple things into
Then you can have (which is what I do):
Because empty items will be omitted, the Response object can (and should) be global for all request. No matter what request is made, it should have a Response object returned.