liveBook · Manning

Map

As explained in Go in Practice, Second Edition

A map is an associative data structure that stores key-value pairs and supports retrieving values by key.

A map is Go’s dictionary-like collection for associating keys with values. Unlike slices and arrays, which serve as list-like collections, maps provide dictionary-style access. section 2.1.3

Maps can help represent valid options, but a slice is also suitable for listing values to validate. For JSON objects with unknown structure, use a map to access fields by key; known structures can instead be parsed into a struct. section 2.1.2 section 11.3

m := f.(map[string]interface{})
fmt.Println(m["firstName"])

After parsing arbitrary JSON into an interface, assert it as map[string]interface{} to retrieve a field such as firstName by key. section 11.3

How Go in Practice, Second Edition covers it

How deep it goes
Defines maps as dictionary-like collections and demonstrates accessing JSON fields through a typed map assertion.
Examples
Uses valid-language options and a parsed JSON object with a firstName field.
What you can do after
Apply maps to retrieve named fields from JSON objects with unknown structure.
How it connects
Connects maps with slices, arrays, JSON, interfaces, and structs.

Also explained in

Compare how 42 books explain Map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.