Skip to the workspace
Pebellslearn by building
Maps & Structs  ›  Lesson

Structs, Tags and JSON

Struct Tags & JSON 16 minA struct groups named fields into one value, and struct tags such as `json:"word,omitempty"` tell encoding/json which key to use for each exported field and when to leave it out.

golang / Structs, Tags and JSON

Explore each scene at your own pace.

01 · The rule

The rule

Structs, Tags and JSON

A struct groups named fields into one value, and struct tags such as `json:"word,omitempty"` tell encoding/json which key to use for each exported field and when to leave it out.

Group named fields and inspect which fields a JSON encoder can access.

tally now has several facts per word: the word, its count, its share of the text. Passing three parallel values around is clumsy. A struct bundles them — and with a few tags, the same struct becomes tally --json output that other programs can read.

The rule

A struct groups fields; tags tell JSON the key; only capitals travel.

Structs. A struct type lists named, typed fields:

type WordStat struct {
	Word  string  `json:"word"`
	Count int     `json:"count"`
	Share float64 `json:"share,omitempty"`
	note  string
}

Create values with field names (order-independent, and missing fields get zero values), then use dot access:

s := WordStat{Word: "go", Count: 3}
s.Count++                // 4
fmt.Println(s.Share)     // 0

Structs are values: t := s copies every field, just like arrays. Two structs of the same type compare with == when all their fields are comparable.

Tags are the back-quoted strings after a field's type. They do nothing by themselves; packages read them. encoding/json understands:

Tag Effect
json:"word" use the key word
json:"share,omitempty" leave the field out when it holds its zero value
json:"-" never include this field
no tag use the field name as the key, "Word"

Only capitals travel. encoding/json can only see exported fields — names starting with a capital letter. note is invisible to it, tag or no tag.

data, err := json.Marshal(WordStat{Word: "go", Count: 3})
fmt.Println(string(data)) // {"word":"go","count":3}

json.Unmarshal goes the other way, filling a struct from JSON; it matches keys to tags (or names) without caring about case.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.