0
0
Goprogramming~10 mins

Common slice operations 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 create a slice of integers with length 3.

Go
numbers := make([]int, [1])
Drag options to blanks, or click blank then click option'
A5
B0
C3
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using the capacity instead of length.
Confusing length with the initial values.
2fill in blank
medium

Complete the code to append the value 7 to the slice named nums.

Go
nums = append(nums, [1])
Drag options to blanks, or click blank then click option'
A0
B7
C5
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Appending a wrong number.
Forgetting to assign the result back to the slice.
3fill in blank
hard

Fix the error in the code to get the first element of the slice data.

Go
first := data[1]
Drag options to blanks, or click blank then click option'
A<0>
B(0)
C{0}
D[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or braces instead of square brackets.
Using an index out of range.
4fill in blank
hard

Fill both blanks to create a new slice sub that contains elements from index 1 to 3 (exclusive) of items.

Go
sub := items[[1]:[2]]
Drag options to blanks, or click blank then click option'
A1
B2
C4
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong end index that includes unwanted elements.
Confusing inclusive and exclusive indexes.
5fill in blank
hard

Fill all three blanks to create a map named lengths where keys are words in words slice and values are their lengths, but only for words longer than 3 characters.

Go
lengths := map[string]int{}

for _, word := range words {
    if len(word) [3] 3 {
        lengths[[1]] = [2]
    }
}
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Mixing up keys and values in the map.