Map — dynamic, runtime-checked key-value store. Struct — static, compile-time-checked collection of named fields.
Maps are ideal when the set of keys isn't known at compile time. They use hash tables internally, giving O(1) average-case lookups. Maps are reference types — passing one to a function shares the same underlying data.
1headers := make(map[string]string)2for _, h := range rawHeaders {3 key, val := strings.SplitN(h, ":", 2)4 headers[strings.TrimSpace(key)] = strings.TrimSpace(val)5}
Structs define a fixed schema the compiler knows about. This enables IDE support, catches errors early, and makes the code self-documenting. Structs compose cleanly via embedding.
1type HTTPRequest struct {2 Method string `json:"method"`3 URL string `json:"url"`4 Headers map[string]string `json:"headers"`5}
When to use:
Common mistakes:
map[string]interface{} instead of proper structs — lose type safety.ok — zero value silently returned.== (except nil check).