Map — dynamic, runtime-checked key-value store with O(1) average lookups. Struct — fixed, compile-time-checked collection of named, typed fields.
Maps are reference types — sharing a map between goroutines requires synchronization. They grow dynamically but have per-entry overhead. Keys must be comparable.
1// Dynamic grouping — great for runtime data2byDept := make(map[string][]Employee)3for _, emp := range employees {4 byDept[emp.Department] = append(byDept[emp.Department], emp)5}
Structs define a compile-time schema. The compiler catches field name typos and type mismatches. Structs are value types — no nil-pointer concerns for the struct itself.
1type Employee struct {2 Name string `json:"name"`3 Department string `json:"department"`4 Salary float64 `json:"salary"`5}
When to use:
Common mistakes:
map[string]interface{} for complex data — use structs.ok check — zero value masks missing data.