When Does Golang\'s `append()` Function Allocate a New Slice?

Patricia Arquette
Release: 2024-11-02 03:27:30
Original
165 people have browsed it

When Does Golang's `append()` Function Allocate a New Slice?

When Does Golang Append() Create a New Slice?

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>
Copy after login

Output:

[1 2 3 4 5]
[1 2 3 4 6]
[1 2 3 4 6]
Copy after login

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>
Copy after login

The output becomes:

[1 2 3 4 5]
[1 2 3 4 6]
[1 2 3 4 5]
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!