メインコンテンツにスキップ
eLearner.app
モジュール 5 · レッスン 4 / 5コース内の 24/50~12 min
モジュールのレッスン (4/5)

フィールドタグと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

Go
type User struct {
    Name string `json:"nome"`
    Age  int    `json:"eta,omitempty"`
}

Format convention: key:"value" separated by spaces (NOT commas) for multiple keys:

Go
type User struct {
    Email string `json:"email" validate:"required,email"`
}

json tag: options

The most common option is json, used by encoding/json:

TagEffect
json:"nome"Renames the field to nome in the JSON output
json:"nome,omitempty"Omits the field if it is at its zero value
json:"-"Do not serialize/deserialize this field
json:",omitempty"Keeps the Go field name but with omitempty
Go
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

Go
// 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/xml
  • yaml:"..." — yaml.v3
  • db:"..." — sqlx / pgx
  • validate:"..." — go-playground/validator
  • form:"..." — gin / echo binding

Libraries read them via reflection (reflect.StructTag.Get("json")): they are just strings until someone interprets them.

Try it

運動#go.m5.l4.e1
試行回数: 0読み込み中…

Add the json:"nome" tag to the Name field and the json:"eta" tag to the Age field.

エディターを読み込み中…
ヒントを表示

Tags are raw strings between backticks right after the field type.

3 回の試行後に解決策が利用可能になります

運動#go.m5.l4.e2
試行回数: 0読み込み中…

Add the omitempty option to the Age field: it must be omitted when it is 0.

エディターを読み込み中…
ヒントを表示

Options come after the name, separated by a comma: json:"nome,omitempty".

3 回の試行後に解決策が利用可能になります

Quiz#go.m5.l4.e3
準備完了

What does it print with tags json:"nome" and json:"eta"?

Go
u := User{Name: "Ada", Age: 36}
b, _ := json.Marshal(u)
fmt.Println(string(b))
回答の選択肢

Recap

  • Tags are raw strings between backticks: `key:"value" key2:"value2"`.
  • json:"nome": rename. omitempty: omit if zero value. -: exclude.
  • NON-exported (lowercase) fields are ignored by encoding/json.
  • json.Marshal(v)json.Unmarshal(data, &v) (pointer).
  • Same mechanism for xml, yaml, db, validate, form, …