Map — dynamic key-value store, runtime-checked. Struct — fixed set of named fields, compile-time checked.
Maps store arbitrary keys of a single type mapped to values of another type. Keys must be comparable (==). Maps are reference types — passing a map to a function shares the same data. They are not safe for concurrent writes without synchronization.
1ages := map[string]int{2 "Alice": 30,3 "Bob": 25,4}5ages["Charlie"] = 35 // add6delete(ages, "Bob") // remove7v, ok := ages["Alice"] // safe lookup
Structs define a fixed schema at compile time. Each field has a name and type — the compiler catches typos and type mismatches. Structs are value types and safe for concurrent reads (though mutation requires synchronization).
1type Person struct {2 Name string3 Age int4}5p := Person{Name: "Alice", Age: 30}6p.Age = 31 // compile-time safe
When to use:
Common mistakes:
map[string]interface{} for everything — lose type safety and discoverability.ok value — silently returns the zero value.sync.Map or a mutex.== — only valid against nil; use reflection or manual comparison for content.