I have a struct:
type Paper struct {
PID int `json:"pID"`
PTitle string `json:"pTitle"`
PDesc string `json:"pDesc"`
PPwd string `json:"pPwd"`
}
Mostly, I will encode the entire struct to JSON. However, occasionally, I need the brief version of the struct; i.e. encode some of the properties, how should I implement this feature?
type BriefPaper struct {
PID int `json:"-"` // not needed
PTitle string `json:"pTitle"`
PDesc string `json:"pDesc"`
PPwd string `json:"-"` // not needed
}
I am thinking of creating a subset struct, something like BriefPaper = SUBSET(Paper)
, but not sure how to implement it in Golang.
I hope I can do something like this:
p := Paper{ /* ... */ }
pBrief := BriefPaper{}
pBrief = p;
p.MarshalJSON(); // if need full JSON, then marshal Paper
pBrief.MarshalJSON(); // else, only encode some of the required properties
Is it possible?