Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does append() modify the provided slice? (See example)

Tags:

go

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.

like image 827
Christian Siegert Avatar asked Jun 30 '13 22:06

Christian Siegert


People also ask

Does append create a new slice?

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.

How do you append to a 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.

How append works in Golang?

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.

Can you append a slice to a slice Golang?

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.

How to perform add modify remove and slice operations on a list?

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'

Which function appends the new element at the end of slice?

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.

How to append an element to a slice in JavaScript?

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.

How to append a slice in Golang?

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.


1 Answers

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.

like image 122
twmb Avatar answered Nov 16 '22 04:11

twmb