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
+7.4k Golang : How to stop user from directly running an executable file?
+7.4k Golang : Rot13 and Rot5 algorithms example
+10.8k Golang : Command line file upload program to server example
+9.3k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+8.4k Golang : How to check if input string is a word?
+18.5k Golang : Iterating Elements Over A List
+6.2k Apt-get to install and uninstall Golang
+10.1k Golang : Bcrypting password
+11.1k Golang : Fix - does not implement sort.Interface (missing Len method)
+17k Golang : XML to JSON example
+22.1k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+9.6k Golang : Quadratic example