Answer the question
In order to leave comments, you need to log in
How to get JSON from a structure in Golang with only given fields?
Let's say there is a structure
type SampleStruct struct {
Foo string `json:"foo"`
Bar string `json:"bar"`
}
example := &SampleStruct{
Foo: "foo foo foo",
Bar: "bar bar bar",
}
{foo: "foo foo foo", bar: "bar bar bar"}
, and if you need to get JSON with only part of the fields, for example, to get this: {foo: "foo foo foo"}
? Is the way out in the creation of additional structures?
Answer the question
In order to leave comments, you need to log in
Bar string `json:"bar,omitempty"`
if Bar is empty, then it will not be in json
type SampleStruct struct {
Foo string `json:"foo"`
Bar string `json:"-"`
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question