0
0
Goprogramming~5 mins

Appending to slices in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a slice in Go?
A slice is a flexible and dynamic view into the elements of an array. It can grow and shrink, unlike arrays which have fixed size.
Click to reveal answer
beginner
How do you append an element to a slice in Go?
Use the built-in append() function. For example: slice = append(slice, element) adds element to the end of slice.
Click to reveal answer
intermediate
What happens if the underlying array of a slice is too small when appending?
Go automatically creates a new, larger array, copies the existing elements, and appends the new element. The slice then points to this new array.
Click to reveal answer
intermediate
Can you append multiple elements to a slice at once? How?
Yes. Use append(slice, elem1, elem2, ...) to add multiple elements. Or append another slice using append(slice, otherSlice...).
Click to reveal answer
beginner
Why must you assign the result of append back to the slice variable?
Because append may return a new slice with a different underlying array, you must assign it back to keep the updated slice reference.
Click to reveal answer
What does the append function return in Go?
AThe original slice unchanged
BA new slice with the added elements
CAn error if the slice is full
DThe underlying array
How do you append all elements of one slice to another?
Aappend(slice1, slice2...)
Bcopy(slice1, slice2)
Cslice1 + slice2
Dappend(slice1, slice2)
If you append to a slice without assigning the result, what happens?
AThe program crashes
BThe slice is updated correctly
CThe original slice remains unchanged
DThe slice is cleared
What is the type of the second argument in append(slice, ...)?
AOnly a single element
BOnly an array
COnly a slice
DA single element or multiple elements
What happens internally when a slice's capacity is exceeded during append?
AA new larger array is allocated and elements copied
BAn error is thrown
CThe slice discards old elements
DThe slice shrinks
Explain how the append function works with slices in Go.
Think about what happens when the slice is full and how you keep the updated slice.
You got /4 concepts.
    Describe how to append multiple elements or another slice to a slice in Go.
    Remember the special syntax for unpacking slices.
    You got /3 concepts.