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
+8.6k Golang : HTTP Routing with Goji example
+11.1k Golang : Post data with url.Values{}
+10.9k CodeIgniter : How to check if a session exist in PHP?
+15.6k Golang : Read large file with bufio.Scanner cause token too long error
+12.5k Golang : http.Get example
+5.1k Swift : Convert string array to array example
+9.4k Golang : Sort and reverse sort a slice of floats
+8.7k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+10.7k Golang : Fix go.exe is not compatible with the version of Windows you're running
+15.8k Golang : How to check if input from os.Args is integer?
+7.1k Golang : Check to see if *File is a file or directory
+9.7k Golang : Setting variable value with ldflags