Maps

Back

Loading concept...

🗺️ Go Maps: Your Digital Dictionary

The Big Picture

Imagine you have a magical notebook where you can find anyone’s phone number instantly. You don’t flip through pages one by one—you just say a name, and poof, the number appears!

That’s exactly what a Map is in Go. It’s like a super-fast lookup book that connects keys (like names) to values (like phone numbers).


🏠 Real-Life Analogy: The Contact Book

Think of your phone’s contact list:

  • Name → finds → Phone Number
  • “Mom” → “555-1234”
  • “Pizza Place” → “555-9999”

Maps work the same way! You give it a key, it gives you back a value. Simple!


📝 Map Declaration

What Is Declaration?

Declaration means telling Go: “Hey, I want a map!” You’re creating an empty box, but you haven’t put anything inside yet.

The Syntax

var myMap map[KeyType]ValueType

Simple Example

// A map where names (strings) point to ages (integers)
var ages map[string]int

// A map where student IDs point to their names
var students map[int]string

Picture This

graph TD A["var ages map"] --> B["Key Type: string"] A --> C["Value Type: int"] B --> D["Names like 'Alice', 'Bob'"] C --> E["Ages like 25, 30"]

Remember: A declared map is nil (empty/nothing) until you initialize it!


🎨 Map Initialization

What Is Initialization?

Initialization means actually creating the map so you can use it. Think of it like:

  • Declaration = “I want a notebook”
  • Initialization = “Here’s your notebook, ready to write!”

Two Ways to Initialize

Way 1: Using make()

ages := make(map[string]int)
// Now ages is ready to use!

Way 2: Map Literal (with values)

ages := map[string]int{
    "Alice": 25,
    "Bob":   30,
    "Carol": 28,
}

Visual Comparison

graph TD A["make#40;map[string]int#41;"] --> B["Empty map, ready to fill"] C["map[string]int{ ... }"] --> D["Map with values already inside"]

Quick Tip 💡

Use make() when you’ll add values later. Use map literals when you already know your data!


⚡ Map Operations

Maps have four superpowers:

1. ADD a Value

ages := make(map[string]int)
ages["Alice"] = 25  // Add Alice with age 25
ages["Bob"] = 30    // Add Bob with age 30

2. READ a Value

aliceAge := ages["Alice"]  // Gets 25
fmt.Println(aliceAge)      // Prints: 25

3. UPDATE a Value

ages["Alice"] = 26  // Alice had a birthday!

4. DELETE a Value

delete(ages, "Bob")  // Bob is gone!

All Together

graph TD A["Map Operations"] --> B["ADD: map[key] = value"] A --> C["READ: value = map[key]"] A --> D["UPDATE: map[key] = newValue"] A --> E["DELETE: delete#40;map, key#41;"]

🔍 Checking Map Key Existence

The Problem

What happens if you ask for someone who doesn’t exist?

age := ages["Zoe"]  // Zoe isn't in our map!
// age is 0 (the zero value for int)

Go doesn’t crash—it just gives you a “zero value.” But how do you know if Zoe actually has age 0, or if she just doesn’t exist?

The Solution: The “Comma OK” Pattern

age, exists := ages["Zoe"]

if exists {
    fmt.Println("Zoe's age:", age)
} else {
    fmt.Println("Zoe not found!")
}

How It Works

graph TD A["ages[key]"] --> B["Returns TWO things"] B --> C["value: the actual value or zero"] B --> D["exists: true or false"] D --> E["true = key exists"] D --> F["false = key not found"]

Pro Pattern

// Check and use in one line
if age, ok := ages["Alice"]; ok {
    fmt.Println("Found Alice:", age)
}

🔄 Map Iteration

Looping Through All Items

Use for...range to visit every key-value pair:

ages := map[string]int{
    "Alice": 25,
    "Bob":   30,
    "Carol": 28,
}

for name, age := range ages {
    fmt.Printf("%s is %d years old\n", name, age)
}

Output (Example)

Alice is 25 years old
Bob is 30 years old
Carol is 28 years old

Only Keys? Only Values?

// Just keys
for name := range ages {
    fmt.Println(name)
}

// Just values (use _ to ignore keys)
for _, age := range ages {
    fmt.Println(age)
}

Visual Flow

graph TD A["for k, v := range map"] --> B["Loop starts"] B --> C["Get first key-value"] C --> D["Do something with k and v"] D --> E["Get next key-value"] E --> F{"More items?"} F -->|Yes| D F -->|No| G["Loop ends"]

🎲 Map Iteration Order

The Surprise!

Maps have NO guaranteed order!

Run this code multiple times:

colors := map[string]string{
    "sky":   "blue",
    "grass": "green",
    "sun":   "yellow",
}

for k, v := range colors {
    fmt.Println(k, "->", v)
}

Different Runs, Different Orders

Run 1:           Run 2:           Run 3:
grass -> green   sky -> blue      sun -> yellow
sky -> blue      sun -> yellow    grass -> green
sun -> yellow    grass -> green   sky -> blue

Why?

Go randomizes map iteration on purpose! This prevents you from accidentally depending on order.

Need Sorted Order?

// 1. Collect keys into a slice
keys := make([]string, 0, len(colors))
for k := range colors {
    keys = append(keys, k)
}

// 2. Sort the keys
sort.Strings(keys)

// 3. Iterate in sorted order
for _, k := range keys {
    fmt.Println(k, "->", colors[k])
}

Remember This! 🧠

graph TD A["Map Iteration Order"] --> B["RANDOM!"] B --> C["Never assume order"] B --> D["Sort keys if you need order"]

☠️ nil Map Behavior

What Is a nil Map?

A map that’s declared but never initialized is nil. It’s like having the idea of a notebook, but no actual notebook!

var ages map[string]int  // Declared but NOT initialized
// ages is nil!

What Works?

Reading returns zero value (doesn’t crash)

age := ages["Alice"]  // Returns 0, no panic

Checking existence works

_, exists := ages["Alice"]  // exists = false

Getting length works

fmt.Println(len(ages))  // Prints: 0

Deleting works (does nothing)

delete(ages, "Alice")  // No crash, does nothing

What BREAKS? 💥

Writing to a nil map causes PANIC!

var ages map[string]int
ages["Alice"] = 25  // 💥 PANIC: assignment to nil map

The Safety Rule

graph TD A["nil Map"] --> B["Reading = Safe ✅"] A --> C["Writing = PANIC! ❌"] A --> D["Delete = Safe ✅"] A --> E["Length = Safe ✅"]

Always Initialize Before Writing!

// WRONG ❌
var ages map[string]int
ages["Alice"] = 25  // PANIC!

// RIGHT ✅
ages := make(map[string]int)
ages["Alice"] = 25  // Works perfectly!

🎯 Quick Summary

Concept What It Means Example
Declaration Tell Go you want a map var m map[string]int
Initialization Create the actual map m = make(map[string]int)
Add/Update Put value in map m["key"] = value
Read Get value from map v := m["key"]
Delete Remove from map delete(m, "key")
Check Existence See if key exists v, ok := m["key"]
Iterate Loop through all for k, v := range m
Order Random each time! Don’t depend on it
nil Map Declared, not initialized Can read, can’t write!

🚀 You Got This!

Maps are one of Go’s most powerful tools. They’re like having a super-fast lookup table that can find anything in an instant!

Remember the Golden Rules:

  1. Always initialize before writing
  2. Use “comma ok” to check if keys exist
  3. Never trust iteration order
  4. nil maps can read, but can’t write

Now go build something amazing! 🎉

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.