Golang : Meaning of omitempty in struct's field tag
Another quick note for myself and maybe useful for you as well. This is about the meaning of the word omitempty in a struct's field tag. Basically, it is used to tell the unmarshaller or marshaller to ignore empty fields.
For example, you created a program that will eat struct data and poop out JSON file. There are some struct data with empty field. Adding the word omitempty
will instruct the the marshaller to skip the empty field and proceed to the next struct record data.
type Employee struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Title string `json:"title,omitempty"`
}
You might want to know about the meaning XML attributes - attr
and how to use it at https://socketloop.com/tutorials/golang-adding-xml-attributes-to-xml-data-or-use-attribute-to-differentiate-a-common-tag-name
Reference :
See also : Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+35.3k Golang : Convert(cast) int64 to string
+18.4k Golang : Close channel after ticker stopped example
+11.2k Golang : convert(cast) string to integer value
+16.8k Golang : Get command line arguments
+44.5k Golang : Marshal and unmarshal json.RawMessage struct example
+7.8k Golang : Random integer with rand.Seed() within a given range
+7.9k Golang : Capture text return from exec function example
+18.6k Golang : How to count the number of repeated characters in a string?
+9.5k Golang : Select region of interest with mouse click and crop from image
+7.4k Swift : Convert (cast) Character to Integer?
+8.5k Golang : How to protect your source code from client, hosting company or hacker?
+13.4k Golang : How to filter a map's elements for faster lookup