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
+12.3k Golang : Simple client-server HMAC authentication without SSL example
+8.7k Golang : Set or add headers for many or different handlers
+6.2k Golang : Get missing location after unmarshal binary and gob decode time.
+37.8k Golang : Converting a negative number to positive number
+5.3k PHP : See installed compiled-in-modules
+51.2k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+10k Golang : Channels and buffered channels examples
+7.2k Golang : Dealing with postal or zip code example
+20.7k Golang : Secure(TLS) connection between server and client
+14.6k Golang : Convert(cast) int to float example
+12.3k Golang : How to display image file or expose CSS, JS files from localhost?
+15.7k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content