0
0
Goprogramming~10 mins

Iterating over maps 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 iterate over the map and print keys.

Go
for [1] := range myMap {
    fmt.Println([1])
}
Drag options to blanks, or click blank then click option'
Aindex
Bkey
Citem
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' instead of 'key' for the first variable.
Using variable names that don't represent keys.
2fill in blank
medium

Complete the code to iterate over the map and print keys and values.

Go
for [1], value := range myMap {
    fmt.Println([1], value)
}
Drag options to blanks, or click blank then click option'
Akey
Bval
Citem
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' or 'value' as the first variable instead of the key.
Using the same variable name for both key and value.
3fill in blank
hard

Fix the error in the code to correctly iterate over the map and print keys and values.

Go
for key, [1] := range myMap {
    fmt.Println(key, [1])
}
Drag options to blanks, or click blank then click option'
Akey
Bitem
Cvalue
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'key' for both variables.
Using variable names that don't represent values.
4fill in blank
hard

Fill both blanks to create a map comprehension that stores lengths of keys longer than 3 characters.

Go
lengths := map[string]int{}
for [1], [2] := range myMap {
    if len([1]) > 3 {
        lengths[[1]] = len([1])
    }
}
Drag options to blanks, or click blank then click option'
Akey
Bvalue
Citem
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable for both blanks.
Using 'item' or 'index' which are not typical for maps.
5fill in blank
hard

Fill all three blanks to create a map that stores uppercase keys with their values if the value is greater than 10.

Go
filtered := map[string]int{}
for [1], [2] := range myMap {
    if [2] > 10 {
        filtered[strings.ToUpper([1])] = [2]
    }
}
Drag options to blanks, or click blank then click option'
Akey
Bvalue
Cval
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using the key variable where the value is needed.