0
0
Goprogramming~10 mins

Accessing map values in Go - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to access the value for key "name" in the map.

Go
person := map[string]string{"name": "Alice", "city": "Paris"}
value := person[[1]]
fmt.Println(value)
Drag options to blanks, or click blank then click option'
A"name"
B"age"
Cname
D"city"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the key string.
Using a variable name without quotes.
2fill in blank
medium

Complete the code to check if the key "age" exists in the map.

Go
person := map[string]int{"age": 30, "score": 100}
value, [1] := person["age"]
if ok {
    fmt.Println("Age is", value)
} else {
    fmt.Println("Age not found")
}
Drag options to blanks, or click blank then click option'
Aok
Bexists
Cfound
Dpresent
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not matching the if condition.
Not using the second value to check existence.
3fill in blank
hard

Fix the error in accessing the map value safely.

Go
scores := map[string]int{"math": 90, "english": 85}
score, [1] := scores["science"]
if ok {
    fmt.Println("Science score:", score)
} else {
    fmt.Println("Science score not found")
}
Drag options to blanks, or click blank then click option'
Afound
Bok
Cexists
Dpresent
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name different from the one in the if condition.
Not assigning the second value from the map access.
4fill in blank
hard

Fill both blanks to create a map of string to int and access the value for key "apples".

Go
inventory := map[string]int[1]: 5, "oranges": 10}
count := inventory[[2]]
fmt.Println(count)
Drag options to blanks, or click blank then click option'
A"apples"
B"bananas"
D"oranges"
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys for map creation and access.
Forgetting quotes around the key strings.
5fill in blank
hard

Fill all three blanks to safely access a map value and print a message if the key "color" exists.

Go
attributes := map[string]string{"color": "blue", "size": "medium"}
value, [1] := attributes[[2]]
if [3] {
    fmt.Println("Color is", value)
} else {
    fmt.Println("Color not found")
}
Drag options to blanks, or click blank then click option'
Aok
B"color"
Dfound
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the existence check.
Not using quotes around the key string.
Using a variable in the if condition that was not assigned.