Map — dynamic key-value store with O(1) lookups, reference type. Struct — fixed set of named, typed fields, compile-time checked, value type.
Maps are reference types — all copies point to the same underlying data. They grow dynamically but are not safe for concurrent writes without synchronization. Keys must be comparable.
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. The compiler catches type errors and typos. Structs are value types and compose via embedding.
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.ok — silently returns zero value.sync.Map or mutex.== — only valid against nil.