1. Maps

Mapsarrow-up-right are similar to JavaScript objects, Python dictionaries, and Ruby hashes. Maps are a data structure that provides key->value mapping.

The zero value of a map is nil.

We can create a map by using a literal or by using the make() function:

example.go
ages := make(map[string]int)
ages["John"] = 37
ages["Mary"] = 24
ages["Mary"] = 21 // overwrites 24
literal.go
ages = map[string]int{
  "John": 37,
  "Mary": 21,
}

The len() function works on a map, it returns the total number of key/value pairs.

len.go
ages = map[string]int{
  "John": 37,
  "Mary": 21,
}
fmt.Println(len(ages)) // 2

Assignment

We can speed up our contact-info lookups by using a map!

  • Key-based map lookup: O(1)

  • Slice brute-force search: O(n)

Complete the getUserMap function. It takes a slice of names and a slice of phone numbers, and returns a map of name -> user structs and an error. A user struct just contains a user's name and phone number. The first element in the names slice pairs with the first phone number, and so on.

If the length of names and phoneNumbers is not equal, return an error with the string "invalid sizes".

Solution

You need to create a user struct that contains both the name and phone number:

Understanding the User Struct

Think of this like a Python class:

To create a new user in Go:

The Corrected Solution

1

Step-by-step algorithm

  • Create an empty map: result := make(map[string]user)

  • If the lengths differ, return nil and errors.New("invalid sizes")

  • Loop over names with index i; for each name create a user with name and phoneNumbers[i]

  • Assign into the map: result[name] = user{...}

  • Return result, nil

2

Example trace

Given:

Iteration 1 (i=0, name="Alice"):

After iteration 1:

Iteration 2 (i=1, name="Bob"):

After iteration 2:

Return: result, nil

Python Comparison

Go:

Python equivalent:

Understanding Maps in Go

Creating a Map

Method 1: Using make

Method 2: Using a literal

Python equivalent:

Accessing Map Values

Python:

Adding/Updating Map Values

Python:

Why Return nil for the Map?

When there's an error, you return:

  • nil is Go's equivalent of Python's None

  • Since we can't create a valid map, we return nil (nothing)

  • The caller will check the error and won't use the map if there's an error

Python equivalent:

Complete Annotated Solution

Key Takeaway

When your map's value is a struct, you need to create the whole struct:

Further Explanation

result = The Container (The Dictionary Itself)

Python equivalent:

user = The Type of Data Being Stored (What Goes IN the Dictionary)

Python equivalent:

Visual Breakdown

Concrete Example with Different Names

The variable name (result, phoneBook, contacts) is YOUR choice. The type (user) is REQUIRED by the map definition.

Putting It All Together

This is like:

Python Analogy

Python with a class:

Python with just dicts (simpler):

In Go, you can't just use {"name": "Alice", "phone": 1234} directly. You need to define a struct type first (user), then create instances of it.

Side-by-Side Comparison

Concept
In Your Code
What It Is

Variable name

result

The map/dictionary you're building

Key type

string

Names are strings

Value type

user

Each value is a user struct

Creating a value

user{...}

Making a new user to put in the map

Another Example to Clarify

  • garage = your variable (the dictionary)

  • car = the type of things you're storing in it

  • car{...} = creating a new car object

Key Takeaway

  • result is like saying "the notebook" - it's the container

  • user is like saying "a person entry" - it's what you write in the notebook