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
+27.3k Golang : dial tcp: too many colons in address
+18.3k Golang : How to get hour, minute, second from time?
+5.8k Golang : Use NLP to get sentences for each paragraph example
+4.5k Javascript : Access JSON data example
+17.7k Golang : Qt image viewer example
+7.7k Golang : Example of how to detect which type of script a word belongs to
+10.8k Nginx : TLS 1.2 support
+12.3k Golang : "https://" not allowed in import path
+15k Golang : How to check if IP address is in range
+16.8k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+30.7k Golang : Interpolating or substituting variables in string examples
+7.6k Golang : Test if an input is an Armstrong number example