Map — dynamic key-value store, runtime-checked, reference type. Struct — fixed set of named fields, compile-time-checked, value type.
Maps grow dynamically and support arbitrary comparable keys. They're reference types — all copies point to the same data. Maps are not safe for concurrent writes.
1env := make(map[string]string)2for _, v := range os.Environ() {3 k, val, _ := strings.Cut(v, "=")4 env[k] = val5}6if port, ok := env["PORT"]; ok {7 fmt.Println("Port:", port)8}
Structs define a fixed schema at compile time. The compiler catches type errors, and IDEs provide autocomplete. Structs are value types.
1type EnvVars struct {2 Port string3 Debug bool4 DBHost string5}
When to use:
Common mistakes:
map[string]interface{} instead of structs.ok check.