Some libraries like encoding/json
use tags for more information in a struct.
Tags can be fetched by using reflect.
Let's check an example:
| type Person struct {
FirstName string `json:"first_name,omitempty" other:"hello"`
}
func main() {
person := Person{
FirstName: "Michael",
}
personType := reflect.TypeOf(person)
if firstNameField, ok := personType.FieldByName("FirstName"); ok {
fmt.Println(firstNameField.Tag.Get("json"))
fmt.Println(firstNameField.Tag.Get("other"))
}
}
|
output:
first_name,omitempty
hello
If you use, for example encoding/json
, you influence how your fields get Marshaled
or Unmarshaled
Source.
We can set the key name of the json object, by using tags.
Let's check an example:
| type Person struct {
FirstName string `json:"first_name,omitempty"`
}
func main() {
person := Person{
FirstName: "Michael",
}
data, err := json.Marshal(person)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(data))
}
|
output:
If we use omitempty
on a field and the field value is set to a zero value, then the field will be truncated:
| person := Person{}
data, err := json.Marshal(person)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(data))
|
output:
If we do not use omitempty
the field will be included always:
| type Person struct {
FirstName string `json:"first_name"`
}
func main() {
person := Person{}
data, err := json.Marshal(person)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(data))
}
|
output: