Maps

Question Click to View Answer

What does the following code print?

package main

import "fmt"

func main() {
    numbers := make(map[string]int)
    numbers["one"] = 1
    numbers["ten"] = 10
    numbers["twenty"] = 20
    fmt.Println(numbers)
}

map[one:1 ten:10 twenty:20] is printed.

The numbers variable is initally assigned to an empty map that's declared to have string keys and integer values.

Three key / value pairs are then added to the numbers map.

The numbers map is then printed.

What does the following code print?

numbers := map[string]int{
    "one":    1,
    "ten":    10,
    "twenty": 20,
}
fmt.Println(numbers)

map[ten:10 twenty:20 one:1] is printed. Maps are unordered, so the ordering of the key / value pairs may be different when this map is printed on your machine.

Maps can be created with a shorthand syntax that's similar to the shorthand syntax used to create arrays and slices.

What does the following code print?

numbers := map[string]int{
    "one":    1,
    "ten":    10,
    "twenty": 20,
}
fmt.Println(len(numbers))

3 is printed.

The numbers map contains 3 key / value pairs.

The len() function returns the number of key / value pairs in a map.

What does the following code print?

numbers := map[string]int{
    "one":    1,
    "ten":    10,
    "twenty": 20,
}
delete(numbers, "ten")
fmt.Println(numbers)

map[twenty:20 one:1] is printed.

The delete() function can be used to delete a key / value pair from a map.

What does the following code print?

places := make(map[string]int)
fmt.Println(places["boo"])

0 is printed.

The places map is declared to have string keys and integer values.

The length of places is zero (i.e. len(places == 0).

When a key doesn't exist in a map, the zero value of the value type is returned. In this example, the value type is an integer and the zero value of an integer is 0, so 0 is returned for the "boo" key that doesn't exist in the map.

What does the following code print?

countryCodes := map[string]string{
    "93":  "Afghanistan",
    "374": "Armenia",
    "61":  "Australia",
}
country, ok := countryCodes["61"]
fmt.Println(country, ok)

Australia true is printed.

The countryCodes map contains string keys and string values.

The country value is assigned to what's fetched by the map and ok indicates if the key is contained in the map or not. In this example, ok is assigned to true because "61" is included in the map.

What does the following code print?

countryCodes := map[string]string{
    "93":  "Afghanistan",
    "374": "Armenia",
    "61":  "Australia",
}
country, ok := countryCodes["999"]
fmt.Println(country == "", ok == false)

true true is printed.

The country variable is assigned to the zero value of the string type because the "999" key isn't included in the map.

The ok variable is assigned to false because the "999" key isn't included in the map.

What does the following code print?

countryCodes := map[string]string{
    "93":  "Afghanistan",
    "374": "Armenia",
    "61":  "Australia",
}
if country, ok := countryCodes["93"]; ok {
    fmt.Println(country, "is a beautiful place")
} else {
    fmt.Println("I don't know that country")
}

Afghanistan is a beautiful place is printed.

The ok variable is assigned to true because the "93" key is included in the map.

What does the following code print?

countryCodes := map[string]string{
    "93":  "Afghanistan",
    "374": "Armenia",
    "61":  "Australia",
}
if country, ok := countryCodes["111"]; ok {
    fmt.Println(country, "is a beautiful place")
} else {
    fmt.Println("I don't know that country")
}

I don't know that country is printed.

The countryCodes map doesn't have a value associated with the "111" key, so ok is assigned to false and the else condition is evaluated.

What does the following code print?

meaning := map[string]map[string]string{
    "red": map[string]string{
        "element": "fire",
        "feeling": "hot",
    },
    "blue": map[string]string{
        "element": "water",
        "feeling": "cold",
    },
}
fmt.Println(meaning["blue"]["feeling"])

"cold" is printed.

The meaning map has string keys and values that are another map.

This is an example of a nested map.