The built-in append function is used to grow slices:
var x []int
x = append(x, 10)
The append function takes a slice of any type and zero or more values of the element type of the slice. It returns a slice with the same type as the passed-in slice, which is usually assigned to the variable that was passed to append. In this example, you are appending to a nil slice, but you can append to a slice that already has elements:
var x = []int{1, 2, 3}
x = append(x, 4)
You can append more than one value at a time:
x = append(x, 5, 6, 7)
One slice is appended onto another by using the... operator to expand the source slice into individual values (you’ll learn more about the... operator in “Variadic Input Parameters and Slices”):
y := []int{20, 30, 40}
x = append(x, y...)
It is a compile-time error if you forget to assign the value returned from append. You might be wondering why as it seems a bit repetitive. I will talk about this in greater detail in the related discussion, but Go is a call-by-value language. Every time you pass a parameter to a function, Go makes a copy of the value that’s passed in. Passing a slice to the append function actually passes a copy of the slice to the function. The function adds the values to the copy of the slice and returns the copy. You then assign the returned slice back to the variable in the calling function.