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
+9.9k Golang : Bcrypting password
+16.2k Golang : Send email and SMTP configuration example
+23.3k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+39.3k Golang : Remove dashes(or any character) from string
+5.2k Unix/Linux/MacOSx : How to remove an environment variable ?
+22.9k Golang : Randomly pick an item from a slice/array example
+7.6k Javascript : How to check a browser's Do Not Track status?
+10k Golang : Random Rune generator
+4.1k Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
+6.7k Golang : How to setup a disk space used monitoring service with Telegram bot
+18.2k Golang : Aligning strings to right, left and center with fill example
+23k Golang : Print out struct values in string format