Go Maps

A map stores key-value pairs. Each key maps to exactly one value. Maps are like a dictionary — look up a word (key) and get its definition (value). Keys must be unique, and lookups happen in constant time regardless of the map size.

Creating a Map

Using a Map Literal

package main

import "fmt"

func main() {
    capitals := map[string]string{
        "India":  "New Delhi",
        "France": "Paris",
        "Japan":  "Tokyo",
    }

    fmt.Println(capitals["India"])  // New Delhi
    fmt.Println(capitals["France"]) // Paris
}

Using make

package main

import "fmt"

func main() {
    ages := make(map[string]int)
    ages["Alice"] = 30
    ages["Bob"] = 25

    fmt.Println(ages) // map[Alice:30 Bob:25]
}

Map Diagram

capitals := map[string]string{...}

Key          Value
─────────────────────────
"India"   →  "New Delhi"
"France"  →  "Paris"
"Japan"   →  "Tokyo"

capitals["India"] → "New Delhi"

Adding and Updating

package main

import "fmt"

func main() {
    scores := map[string]int{}

    scores["Alice"] = 90   // add
    scores["Bob"] = 85     // add
    scores["Alice"] = 95   // update

    fmt.Println(scores) // map[Alice:95 Bob:85]
}

Deleting a Key

package main

import "fmt"

func main() {
    colors := map[string]string{
        "red":   "#FF0000",
        "green": "#00FF00",
        "blue":  "#0000FF",
    }

    delete(colors, "green")
    fmt.Println(colors) // map[blue:#0000FF red:#FF0000]
}

Checking If a Key Exists

A map lookup returns two values: the value and a boolean. If the key does not exist, the boolean is false and the value is the zero value of that type.

package main

import "fmt"

func main() {
    population := map[string]int{
        "Mumbai":  20000000,
        "Delhi":   32000000,
    }

    val, ok := population["Delhi"]
    if ok {
        fmt.Println("Delhi population:", val)
    }

    val2, ok2 := population["London"]
    if !ok2 {
        fmt.Println("London not found, default:", val2) // 0
    }
}

Output:

Delhi population: 32000000
London not found, default: 0

Iterating Over a Map

package main

import "fmt"

func main() {
    grades := map[string]string{
        "Alice": "A",
        "Bob":   "B",
        "Carol": "A+",
    }

    for name, grade := range grades {
        fmt.Printf("%s scored %s\n", name, grade)
    }
}

Map iteration order is not guaranteed — the output may appear in any order each time the program runs.

Map of Slices

Maps can hold slices as values, enabling one key to point to multiple items.

package main

import "fmt"

func main() {
    courses := map[string][]string{
        "Alice": {"Go", "Python", "SQL"},
        "Bob":   {"Java", "React"},
    }

    fmt.Println(courses["Alice"]) // [Go Python SQL]
}

Key Points

  • Maps store key-value pairs; keys must be unique
  • Use make(map[K]V) or a map literal to create a map
  • Looking up a non-existent key returns the zero value — not an error
  • Always use the two-value lookup val, ok := to check if a key exists
  • Map iteration order is random — do not rely on it
  • Use delete(m, key) to remove a key from a map

Leave a Comment