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
+5.2k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+5.6k Setting $GOPATH environment variable for Unix/Linux and Windows
+8.2k Golang : Generate DSA private, public key and PEM files example
+62.6k Golang : How to return HTTP status code?
+8.5k Golang : Forwarding a local port to a remote server example
+8.3k Golang : Decompress zlib file example
+8.3k Swift : Convert (cast) String to Integer
+14.8k Android Studio : AlertDialog and EditText to get user string input example
+7.8k Golang : Wait and sync.WaitGroup example
+5.1k Golang : Ways to recover memory during run time.
+10.2k Golang : Simple word wrap or line breaking example
+20.8k Golang : GORM read from database example