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)
}
|
The Three key / value pairs are then added to the The |
What does the following code print? numbers := map[string]int{
"one": 1,
"ten": 10,
"twenty": 20,
}
fmt.Println(numbers)
|
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))
|
The The |
What does the following code print? numbers := map[string]int{
"one": 1,
"ten": 10,
"twenty": 20,
}
delete(numbers, "ten")
fmt.Println(numbers)
|
The |
What does the following code print? places := make(map[string]int)
fmt.Println(places["boo"])
|
The The length of 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)
|
The The |
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)
|
The The |
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")
}
|
The |
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")
}
|
The |
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"])
|
The This is an example of a nested map. |