Start learning free

LESSON 131 OF 285 · DICTIONARIES - CHALLENGES

Phone Book Lookup in Go: Reading a Map by Key

Practice Read the full explanation

Given a phone book phoneBook, a map from names to phone numbers, and a name, return the phone number of name.

Every name in the phone book is a key, and its value is that person's phone number, a string like "555-0142". The name you look up is always in the phone book.

 

Example 1:

Input: phoneBook = {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"}, name = "Ben"
Output: "555-0199"
Explanation: The key "Ben" maps to "555-0199".

Example 2:

Input: phoneBook = {"Max": "555-0100"}, name = "Max"
Output: "555-0100"
Explanation: The phone book has a single entry, and it is the one we look up.

Example 3:

Input: phoneBook = {"Tom": "555-0111", "Tim": "555-0111"}, name = "Tim"
Output: "555-0111"
Explanation: Tom and Tim share a phone number. We only need the value of the key "Tim".

 

Constraints:

  • phoneBook has at least one entry.
  • name is always a key of phoneBook.
  • Names are single words made of English letters, and they are all different. Two people can share a phone number.
  • Every phone number is a string of three digits, a dash and four digits, like "555-0142".
STEP 1 OF 1
    Ctrl + Enter to check

    GO · DICTIONARIES - CHALLENGES

    Phone Book Lookup in Go, explained

    Problem overview

    The function phoneBookLookup receives phoneBook, a map[string]string from names to phone numbers, and a name. It returns the phone number stored under that name. The name is always a key of the map, so there is no missing case to handle.

    For {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"} and name = "Ben", the answer is "555-0199". When two people share a number, as in {"Tom": "555-0111", "Tim": "555-0111"}, only the key matters: looking up "Tim" gives "555-0111".

    Approach

    A map is a collection of key-value pairs, and reading a value takes the key in square brackets: phoneBook[name]. The lookup doesn't scan the map, so it's fast whatever its size. The lesson has one step: return that expression.

    Step by step

    1. Read the type. The map goes from string to string, so the key you pass is a string and the value you get back is a string.
    2. Index the map with name and return the result. One expression, no loop and no if, because the key is guaranteed to exist.

    The solution in Go

    func phoneBookLookup(phoneBook map[string]string, name string) string {
        return phoneBook[name]
    }
    

    Things to notice:

    • The key is a variable. phoneBook[name] uses the string held in name. Writing phoneBook["name"] looks for a key that is the word name and finds nothing.
    • The value type is the return type. The map holds strings, so the expression is a string, matching the function's result.
    • A missing key doesn't fail. Reading a key that isn't there gives the zero value of the value type, here the empty string. With the map above, phoneBook["Zed"] is "", and reading from a nil map gives "" too. This lesson never needs it, but it means a wrong spelling returns an empty string instead of stopping the program.

    More about maps

    A few operations on the same kind of map, for reference. In a file of your own:

    phoneBook := map[string]string{"Ana": "555-0142", "Ben": "555-0199"}
    phoneBook["Dee"] = "555-0170" // adds a new key
    phoneBook["Ben"] = "555-0000" // replaces the value of an existing key
    number, found := phoneBook["Zed"]
    delete(phoneBook, "Ana")
    

    After the first two lines, len(phoneBook) is 3 and phoneBook["Ben"] is 555-0000. For the missing name, number is "" and found is false. After delete, the map prints as map[Ben:555-0000 Dee:555-0170].

    Keys are unique: assigning to an existing key replaces its value, which is why the lesson says all names are different. Values are not unique, so two names can share a number, and the lookup by name never gets confused by that. Going the other way, from a number to a name, would need a loop, because a map only searches by key.

    Edge cases and common mistakes

    • Spelling and case. Keys match exactly: "ben" is not "Ben".
    • Single-entry maps. A map with one entry, such as {"Max": "555-0100"}, needs no special case: the same lookup returns "555-0100". Don't assume that the first entry is the one you want in a larger map either, because Go doesn't promise any iteration order.
    • Quoting the variable. Use name, not "name".
    • Looping to find the key. Going through the map with range works but is unnecessary, because the bracket lookup is the direct way.

    Testing

    • {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"}, "Ben" gives 555-0199.
    • {"Tom": "555-0111", "Tim": "555-0111"}, "Tim" gives 555-0111.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free