Start learning free

LESSON 130 OF 671 · DICTIONARIES

Reading a Missing Key in Go: Zero Value Defaults

Lesson Read the full explanation

TL ; DR:

A missing key gives the zero value, 0 for an int. So m[key] already reads with 0 as the default.


points := map[string]int{"Ana": 7}

fmt.Println(points["Ana"])   // 7
fmt.Println(points["Ben"])   // 0


Full lesson:

A missing key often means "none yet". A player with no points yet has 0 points.

As Maps & the ok Idiom showed, Go never fails on a missing key.

A lookup like points["Ben"] gives 0, the zero value of an int.

So when 0 is the default you want, a plain lookup is enough.

Reading this way doesn't add the key. points still has one key afterwards.

The zero value of a string is "", and of a bool it is false.

When you need another default, use comma-ok. Here a missing key gives -1:

score, ok := points["Ben"]
if !ok {
    score = -1
}

Your task

stock counts the fruit in a shop. The code reads and prints the pears.

There are no plums yet. Read "plums" with a plain lookup. Store it in plums.

Then print the plums line, the same way as the pears line.

Expected output:

We have 2 pears
We have 0 plums
STEP 1 OF 2
    Ctrl + Enter to check

    GO · DICTIONARIES

    Dictionary Default Value in Go, explained

    A missing key means "none yet"

    Often a key that is not in the map has a natural meaning. A shop with no plums has 0 plums. A word that hasn't been counted yet has been seen 0 times. Go makes that case easy: reading a missing key never fails, and it gives the zero value of the value type.

    stock := map[string]int{"apples": 5}
    fmt.Println(stock["apples"])   // 5
    fmt.Println(stock["plums"])    // 0
    

    The zero value of an int is 0, so a plain lookup already reads with 0 as the default. No if is needed.

    Reading doesn't add the key

    A lookup only reads. After stock["plums"], the map still has exactly one key: len(stock) is still 1. Only an assignment such as stock["plums"] = 3 adds a key.

    Zero values by type

    The default depends on the value type of the map:

    Value typeMissing key gives
    int0
    string"" (empty text)
    boolfalse

    Printing names["a"] on an empty map[string]string shows an empty string, and flags["a"] on a map[string]bool shows false.

    This also makes a counter easy to write. For a key that doesn't exist yet, stock["plums"] = stock["plums"] + 3 reads 0, adds 3 and stores the result. The map then has 2 keys and stock["plums"] is 3. No separate step to create the key is needed.

    When you want a different default

    The zero value isn't always the right answer. To get something else, use the comma-ok form and replace the value when the key is missing. Here a missing key gives -1:

    plums, ok := stock["plums"]
    if !ok {
        plums = -1
    }
    fmt.Println(plums)   // -1
    

    The same form is how you tell a missing key from a key that really stores a zero. If stock["pears"] was set to 0, a plain lookup gives 0 for it, just as it does for "figs", which was never added. When the difference matters, check ok.

    The task: pears and plums

    stock counts the fruit in a shop: 5 apples and 2 pears, and no plums. The starter code already reads and prints the pears. Your job is to do the same for the plums.

    1. Read "plums" with a plain lookup and store it in plums with :=. The key is missing, and the zero value, 0, is exactly the default we want.
    2. Print the plums line the same way as the pears line, with plums in place of pears.
    stock := map[string]int{"apples": 5, "pears": 2}
    pears := stock["pears"]
    fmt.Println("We have", pears, "pears")
    plums := stock["plums"]
    fmt.Println("We have", plums, "plums")
    

    The output is:

    We have 2 pears
    We have 0 plums
    

    Common mistakes

    • Expecting an error or a crash for a missing key. Go gives the zero value and carries on.
    • Assuming a printed 0 means the key exists. It may be a missing key, so check ok when that matters.
    • Copying the pears line and forgetting to change the name in the text, which prints the wrong label.

    Key takeaways

    • A missing key gives the zero value of the value type: 0, "" or false.
    • When that is the default you want, a plain m[key] is enough, and it doesn't add the key.
    • For another default, or to tell a missing key from a stored zero, use the comma-ok form.
    Keep going with Go500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free