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.5k Golang : Another camera capture GUI application with GTK and OpenCV
+30.6k Golang : Copy directory - including sub-directories and files
+18.3k Golang : Example for DSA(Digital Signature Algorithm) package functions
+11.3k Golang : Listen and Serve on sub domain example
+7k Swift : Convert (cast) String to Float
+4.5k PHP : Fix Call to undefined function curl_init() error
+11k Golang : List running EC2 instances and descriptions
+15.2k Golang : Check if a string contains multiple sub-strings in []string?
+7k Golang : Get final or effective URL with Request.URL example
+28.1k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+4.7k Swift : Get substring with rangeOfString() function example
+6.2k Golang : Validate credit card example