2.3 KiB
2.3 KiB
The Mechanics of Append Quiz
Which append call below allocates a new backing array for the following slice?
words := []string{"lucy", "in", "the", "sky", "with", "diamonds"}
- words = append(words[:3], "crystals")
- words = append(words[:4], "crystals")
- words = append(words[:5], "crystals")
- words = append(words[:5], "crystals", "and", "diamonds") CORRECT
1: No, it just overwrites the 4th element.
2: No, it just overwrites the 5th element.
3: No, it just overwrites the last element.
4: Yes, it overwrites the last element, then it adds two element. However, there is not enough space to do that, so it allocates a new backing array.
What does the program print?
words := []string{"lucy", "in", "the", "sky", "with", "diamonds"}
words = append(words[:1], "is", "everywhere")
words = append(words, words[len(words)+1:cap(words)]...)
- lucy in the sky with diamonds
- lucy is everywhere in the sky with diamonds
- lucy is everywhere with diamonds CORRECT
- lucy is everywhere
3: line #2 overwrites the 2nd and 3rd elements. line #3 appends ["with" "diamonds"] after the ["lucy" "is" "everwhere"].
What are the length and capacity of the words slice?
// The words slice has 1023 elements.
//
// Tip: The keyed slice works like the same as a keyed array.
// If you don't remember how it works, please check out the keyed elements in the arrays section.
//
words := []string{1022: ""}
words = append(words, "boom!")
- Length: 1024 - Capacity: 1024
- Length: 1025 - Capacity: 1025
- Length: 1025 - Capacity: 1280
- Length: 1024 - Capacity: 2048 CORRECT
4: That's right! Append function grows by doubling the capacity of the previous slice.
What are the length and capacity of the words slice?
// The words slice has 1024 elements.
//
// Tip: The keyed slice works like the same as a keyed array.
// If you don't remember how it works, please check out the keyed elements in the arrays section.
//
words := []string{1023: ""}
words = append(words, "boom!")
- Length: 1024 - Capacity: 1024
- Length: 1025 - Capacity: 1025
- Length: 1025 - Capacity: 1280 CORRECT
- Length: 1025 - Capacity: 2048
3, 4: After 1024 elements, the append function grows at a slower rate, about 25%.