encoding/json is Go’s standard-library package for converting structured JSON data to and from Go values. It supports json.Decoder and json.Unmarshal for decoding, plus json.Encoder and json.Marshal for encoding. section 3.5
Listing 3.4 bookworm.go: Bookworm and Book structures
// A Bookworm contains the list of books on a bookworm's shelf.
type Bookworm struct {
Name string `json:"name"` #1
Books []Book `json:"books"` #2
}
// Book describes a book on a bookworm's shelf.
type Book struct {
Author string `json:"author"`
Title string `json:"title"`
}
Define exported Go structures whose fields match the JSON shape, using tags such as `json:"name"` and `json:"books"` to map field names. section 3.1.3
A practical file-decoding workflow:
- Keep destination fields exported so the decoder can write to them; store test JSON under `testdata`. section 3.1.3 section 3.5
Loading bookworms
Calling `loadBookworms("testdata/bookworms.json")` returns bookworms and their books, or prints an error and exits. Running `go run .` produces entries such as Fadi with Margaret Atwood’s The Handmaid's Tale and Sylvia Plath’s The Bell Jar.

