Map — dynamic key-value store, O(1) lookups, reference type. Struct — fixed named fields, compile-time checked, value type.
Maps are ideal for dynamic data. They grow automatically and support delete. Not safe for concurrent writes.
1counters := make(map[string]int)2for _, word := range words {3 counters[word]++4}
Structs define a compile-time schema. The compiler catches errors. They compose via embedding.
1type Stats struct {2 WordCount map[string]int3 Total int4}
When to use:
Common mistakes:
map[string]interface{} instead of structs.ok check.