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
+6.5k Golang : Trim everything onward after a word
+16.7k Golang : How to remove certain lines from a file
+16.9k Golang : How to log each HTTP request to your web server?
+9.2k Golang : Channels and buffered channels examples
+8.5k Golang : Detect number of active displays and the display's resolution
+6k Golang : Get expvar(export variables) to work with multiplexer
+12.7k Golang : How to get year, month and day?
+12.1k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+18.8k Golang : Append content to a file
+3.9k Linux : sudo yum updates not working
+7.2k Swift : Convert (cast) String to Float
+5.2k PHP : How to check if an array is empty ?