DAY 4 — Maps and Zero Values

Goal: Learn how Go represents key–value data and how zero values affect program correctness.


  1. What a Map Is

Example: map[string]int


  1. Declaring and Creating Maps

Valid ways to create maps:

Invalid:

Rule: A map must be initialized before writing to it.


  1. Reading From a Map

Example: m["x"] returns 0 if value type is int.

Important: You cannot tell if the key existed just from the value.


  1. The “Comma ok” Pattern

Rules:

This is mandatory when correctness depends on key presence.


  1. Writing to a Map

Example: m["a"] = 10

If the key already exists, the value is replaced.


  1. Deleting From a Map

delete(m, "x") is always valid.


  1. Iterating Over a Map

Syntax: for k, v := range m { ... }

Rule: Map iteration order is intentionally randomized.


  1. Maps Are Reference Types

Rule: You do NOT need pointers to modify a map.

This is different from structs.


  1. Zero Value of a Map

Rule: Always initialize maps before writing.


  1. When to Use Maps vs Slices

Use a map when:

Use a slice when:


End of Day 4 Append key learnings to learnt.txt after completion.