Lições do módulo (4/5)
Tags de campo e JSON
Tags on struct fields are metadata strings, read via reflection
by libraries like encoding/json, encoding/xml, validators, ORMs. They
are written between backticks (raw string) right after the field's type.
Syntax
type User struct {
Name string `json:"nome"`
Age int `json:"eta,omitempty"`
}Format convention: key:"value" separated by spaces (NOT
commas) for multiple keys:
type User struct {
Email string `json:"email" validate:"required,email"`
}json tag: options
The most common option is json, used by encoding/json:
QUERY LENGTH LIMIT EXCEEDED. MAX ALLOWED QUERY : 500 CHARS
type User struct {
Name string `json:"nome"`
Age int `json:"eta,omitempty"`
Password string `json:"-"`
}
u := User{Name: "Ada", Age: 0, Password: "segreto"}
b, _ := json.Marshal(u)
fmt.Println(string(b)) // {"nome":"Ada"}Age is omitted (zero value 0 + omitempty); Password is
excluded entirely.
Marshal and Unmarshal
// struct -> JSON
b, err := json.Marshal(u)
// JSON -> struct (needs a POINTER)
var u2 User
err = json.Unmarshal(b, &u2)Unmarshal only accepts a pointer, because it must write into the
value's fields.
Other common tags
xml:"..."—encoding/xmlyaml:"..."— yaml.v3db:"..."— sqlx / pgxvalidate:"..."— go-playground/validatorform:"..."— gin / echo binding
Libraries read them via reflection (reflect.StructTag.Get("json")):
they are just strings until someone interprets them.
Try it
Add the json:"nome" tag to the Name field and the json:"eta" tag to the Age field.
Mostrar dica
Tags are raw strings between backticks right after the field type.
Solução disponível após 3 tentativas
Add the omitempty option to the Age field: it must be omitted when it is 0.
Mostrar dica
Options come after the name, separated by a comma: json:"nome,omitempty".
Solução disponível após 3 tentativas
What does it print with tags json:"nome" and json:"eta"?
u := User{Name: "Ada", Age: 36}
b, _ := json.Marshal(u)
fmt.Println(string(b))Recap
QUERY LENGTH LIMIT EXCEEDED. MAX ALLOWED QUERY : 500 CHARS