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
+3.8k Unix/Linux : How to pipe/save output of a command to file?
+40.6k Golang : Get hardware information such as disk, memory and CPU usage
+31.7k Golang : Converting a negative number to positive number
+32.6k Golang : convert(cast) bytes to string
+6.6k Gogland : Where to put source code files in package directory for rookie
+4.5k Golang : Get S3 or CloudFront object or file information
+4.9k Golang : Compound interest over time example
+10.4k Golang : Change date format to yyyy-mm-dd
+45.3k Golang : Convert int to byte array([]byte)
+5.2k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+8.9k Golang : Use regular expression to get all upper case or lower case characters example
+7.3k Golang : Convert word to its plural form example