Contrary to the built-in API documentation, the append() function does not always create a new slice. This is determined by the capacity of the original slice and the number of elements to be added.
In the provided code snippet, where combinations of booleans are recursively created, the exclaimation marks indicate the slices sent into the channel from the AddOption function. However, the slices that emerge on the other side in main() are modified.
The confusion arises from the distinction between the slice data type and its internal representation. The slice descriptor consists of two integers (length and capacity) and a pointer to the underlying data. Therefore, append() returns a new slice with a new descriptor but the same pointer to data.
This is illustrated in the following code sample:
<code class="go">package main import "fmt" func main() { s := make([]int, 0, 5) // Create a slice with zero length and capacity 5 s = append(s, []int{1, 2, 3, 4}...) // Append a slice of integers a := append(s, 5) // Append one element (slice descriptor and data pointer remain the same) fmt.Println(a) b := append(s, 6) // Append one element (slice descriptor and data pointer remain the same) fmt.Println(b) fmt.Println(a) }</code>
Output:
[1 2 3 4 5] [1 2 3 4 6] [1 2 3 4 6]
This demonstrates that both a and b share the same data pointer because the original slice still has capacity. However, if we reduce the capacity to 4:
<code class="go">s := make([]int, 0, 4)</code>
The output becomes:
[1 2 3 4 5] [1 2 3 4 6] [1 2 3 4 5]
Since the original slice no longer has sufficient capacity, append() creates a new slice with a new data pointer for b.
The above is the detailed content of When Does Golang\'s `append()` Function Allocate a New Slice?. For more information, please follow other related articles on the PHP Chinese website!