16-slices/questions/7-mechanics-of-append.md
words := []string{"lucy", "in", "the", "sky", "with", "diamonds"}
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.
words := []string{"lucy", "in", "the", "sky", "with", "diamonds"}
words = append(words[:1], "is", "everywhere")
words = append(words, words[len(words)+1:cap(words)]...)
3: line #2 overwrites the 2nd and 3rd elements. line #3 appends ["with" "diamonds"] after the ["lucy" "is" "everwhere"].
// 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!")
4: That's right! Append function grows by doubling the capacity of the previous 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!")
3, 4: After 1024 elements, the append function grows at a slower rate, about 25%.