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
- Read the type. The map goes from
stringtostring, so the key you pass is a string and the value you get back is a string. - Index the map with
nameand return the result. One expression, no loop and noif, 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 inname. WritingphoneBook["name"]looks for a key that is the wordnameand 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 anilmap 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
rangeworks but is unnecessary, because the bracket lookup is the direct way.
Testing
{"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"},"Ben"gives555-0199.{"Tom": "555-0111", "Tim": "555-0111"},"Tim"gives555-0111.
