0
0
Goprogramming~10 mins

Why maps are used in Go - Test Your Understanding

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 ages [1]
Drag options to blanks, or click blank then click option'
A[]int
Bmap[string]int
Cstruct{}
Dchan int
Attempts:
3 left
💡 Hint
Common Mistakes
Using slice syntax instead of map syntax.
Trying to declare a map without specifying key and value types.
2fill in blank
medium

Complete the code to initialize a map using the make function.

Go
scores := [1](map[string]int)
Drag options to blanks, or click blank then click option'
Amake
Bcreate
Cnew
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Using new instead of make for maps.
Trying to initialize maps without using make or a literal.
3fill in blank
hard

Fix the error in the code to correctly add a key-value pair to the map.

Go
m := make(map[string]int)
m[[1]] = 10
Drag options to blanks, or click blank then click option'
Am
Bage
C10
D"age"
Attempts:
3 left
💡 Hint
Common Mistakes
Using an integer or variable without quotes as a string key.
Using the map variable itself as a key.
4fill in blank
hard

Fill both blanks to create a map comprehension that stores word lengths for words longer than 3 characters.

Go
lengths := map[string]int{
	[1]: len([2]) for _, [2] := range words if len([2]) > 3
}
Drag options to blanks, or click blank then click option'
A"word"
Bword
Cwords
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using the slice words as a key.
Not quoting the key string.
5fill in blank
hard

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

Go
if val, [1] := m[[2]]; [3] {
	fmt.Println(val)
}
Drag options to blanks, or click blank then click option'
Aok
B"key"
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using the value variable instead of the boolean check.
Not quoting the key string.