Map — dynamic key-value store with O(1) lookups, runtime-checked. Struct — fixed, compile-time-checked collection of named fields.
Maps are perfect for dynamic data where keys aren't known ahead of time. They grow automatically and support delete. Maps are reference types — sharing between goroutines needs synchronization.
1cache := make(map[string]*Product)2for _, p := range products {3 cache[p.SKU] = p4}5delete(cache, "DISCONTINUED-SKU")6if p, ok := cache[sku]; ok {7 fmt.Println(p.Name)8}
Structs define a compile-time schema — the compiler catches type errors, IDEs provide autocomplete, and memory layout is predictable. Structs compose via embedding.
1type Product struct {2 SKU string `json:"sku"`3 Name string `json:"name"`4 Price float64 `json:"price"`5}
When to use:
Common mistakes:
map[string]interface{} instead of structs — lose type safety.ok check — zero value masks missing data.