Just a simple question about returning part of a stuct

type Report struct {
SysrptID
Name string
Description string
FileName string
RptType string
}

so this is my first ‘micro service’ in go and for a get I am returning json that represents the above struct.

In most cases I return most of the struct and I know I can keep JSON from including fields I may not need. My question is if I need some responses just to show the name and the SysrptID, what is the best way to do that?

My service is restful; should I create a new route just for this like /ids instead of reports, or I guess I could put an id type in the existing struct. I am using the gorilla web toolkit if that matters. Just looking for ideas, since this is kind of new to me.

so my thinking is instead of showing everything and letting the client deal with all if it, I might wand a way to show an id and a name and then if the client wants details; they pass the id. is it a good idea?

IMHO the simplest way is creating another lighter struct with minimum required fields. You can add a url parameter to the route to choose which to send to the client or you can create another route for lighter struct.

2 Likes

In this case, just return the json with those two fields. There is really no need to overcomplicate things.
https://play.golang.org/p/OZ2H1Zlvcop

1 Like

nice

Using JSON tag omitempty will maintain a unified data serialization method.

https://play.golang.org/p/PnpZqSI6Enj

1 Like

nicer :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.