You can run the example code on Go Playground.
Here is the code:
package main
import "fmt"
func main() {
numbers := []int{1, 2, 3, 4, 5}
fmt.Println(numbers)
_ = append(numbers[0:1], numbers[2:]...)
fmt.Println(numbers)
}
Output:
[1 2 3 4 5]
[1 3 4 5 5]
Why was the numbers
slice modified by append? Is this expected behavior and if yes, could you explain to me why? I thought append
doesn't modify its arguments.
The append built-in function appends elements to the end of a slice. If it has sufficient capacity, the destination is resliced to accommodate the new elements. If it does not, a new underlying array will be allocated. Append returns the updated slice.
Since slices are dynamically-sized, you can append elements to a slice using Golang's built-in append method. The first parameter is the slice itself, while the next parameter(s) can be either one or more of the values to be appended.
The append method in go allows you to add any number of specified items to the end of a slice. If the underlying array contains enough capacity, go use this array.
When it comes to appending a slice to another slice, we need to use the variadic function signature dots. In variadic functions in Golang, we can pass a variable number of arguments to the function and all the numbers can be accessed with the help of the argument name.
To perform add, append, modify, remove and slice operations on a list. Define a list lst = [] with some sample items in it. Find the length of the list using len () function. Append a item to the list using append () function. Change value of an item in a list by specifying an index and its value lst [n] = 'some value'
This function appends the new element at the end of the slice. Here, this function takes s slice and x…T means this function takes a variable number of arguments for the x parameter. Such type of function is also known as a variadic function.
It is just like an array having an index value and length, but the size of the slice is resized they are not in fixed-size just like an array. As we already know that slice is dynamic, so the new element can be appended to a slice with the help of append () function.
To append a slice in Golang, use the append () function. The append () is a built-in Go function used to append elements to a slice. As per the official Go documentation, the following is the signature of the append () function from the built-in package.
See http://blog.golang.org/go-slices-usage-and-internals.
The append function could allocate a new underlying array if what you are appending to the slice does not fit in the current slice's capacity. Append does modify the underlying array. The reason you have to assign back to the variable is because, as I said in the first sentence, the underlying array could be reallocated and the old slice will still point to the old array.
See this play example to see exactly what I am talking about.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With