0
0
Goprogramming~10 mins

Map use cases 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 declare a map that stores string keys and int values.

Go
var scores map[string][1]
Drag options to blanks, or click blank then click option'
Afloat64
Bstring
Cint
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using string as the value type instead of int.
Confusing key and value types.
2fill in blank
medium

Complete the code to initialize the map before adding elements.

Go
scores := make(map[string][1])
Drag options to blanks, or click blank then click option'
Afloat64
Bstring
Cbool
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using string or other types instead of int.
Forgetting to initialize the map before use.
3fill in blank
hard

Fix the error in the code that adds a new key-value pair to the map.

Go
scores[[1]] = 90
Drag options to blanks, or click blank then click option'
A"Alice"
BAlice
C90
Dscore
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted string keys.
Using a number or variable name instead of a string literal.
4fill in blank
hard

Fill both blanks to check if a key exists in the map and print its value.

Go
value, [1] := scores["Bob"]
if [2] {
    fmt.Println("Bob's score:", value)
}
Drag options to blanks, or click blank then click option'
Aok
Bexists
Ctrue
Dfound
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in assignment and condition.
Using a string or non-boolean variable in the if condition.
5fill in blank
hard

Fill all three blanks to declare the words slice, initialize the lengths map, and populate it with lengths for words longer than 3 characters.

Go
[1] := []string{"go", "code", "map", "function"}
[2] := make(map[string][3])
for _, w := range [1] {
    if len(w) > 3 {
        [2][w] = len(w)
    }
}
Drag options to blanks, or click blank then click option'
Awords
Blengths
Cint
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping slice and map variable names.
Using string instead of int for map values.