16-slices/questions/2-appending.md
1: Yes, the append function doesn't change the given slice unless you overwrite the result of the append function back to the original slice. Most of the times, this is true.
2: It doesn't return the existing slice, it returns a new slice. That's why you usually save the result of the append back to the original slice.
3: It doesn't change the given slice, it creates and returns a new slice. Most of the times, this is true.
3: Yes! The append function appends the new elements by looking at the length of the given slice and then returns a new slice with the newly appended elements.
nums := []int{9, 7, 5}
append(nums, []int{2, 4, 6}...)
fmt.Println(nums[3])
3: That's right. The append function returns a new slice with the newly added elements. But here, the code doesn't save the result of the append, so the nums slice only has 3 elements. So,
nums[3]is invalid, because there are only 3 elements in the nums slice.
nums := []int{9, 7, 5}
evens := append(nums, []int{2, 4, 6}...)
fmt.Println(nums, evens)
2: It appends the new elements to the nums slice but it saves the returned slice to the evens slice. So, the nums slice doesn't change. That's why, it prints the original elements from the nums slice first, then it prints the evens slice with the newly added elements.
3, 4: It doesn't save the result of the append call back into the nums slice, so the nums slice doesn't contain the new elements.
nums := []int{9, 7, 5}
nums = append(nums, 2, 4, 6)
fmt.Println(nums)
1: It overwrites the nums slice with the new slice that is returned from the append function. So the nums slice has got the newly appended elements.