In der Go-Sprache ist Slice leistungsfähiger, flexibler und bequemer als ein Array und eine leichtgewichtige Datenstruktur. Ein Slice ist eine Sequenz variabler Länge, die Elemente eines ähnlichen Typs speichert, es ist Ihnen nicht erlaubt, unterschiedliche Typen von Elementen in demselben Slice zu speichern. Es ist wie ein Array mit einem Indexwert und einer Länge, aber die Größe des Segments wird geändert, sie haben nicht wie ein Array eine feste Größe.
Da wir bereits wissen, dass Slice dynamisch ist, kann das neue Element mit Hilfe der Funktion append() an ein Slice angehängt werden. Diese Funktion hängt das neue Element am Ende des Slice an.

Syntax:

func append(s []T, x ...T) []T

Hier nimmt diese Funktion s Slice und x…T bedeutet, dass diese Funktion eine variable Anzahl von Argumenten für den x -Parameter akzeptiert. Eine solche Funktion wird auch als variadische Funktion bezeichnet.

Wenn der Slice durch das Array unterstützt wird und Arrays eine feste Länge haben, wie ist es dann möglich, dass ein Slice eine dynamische Länge hat?

Nun, die Antwort ist, wenn die neuen Elemente an das Slice angehängt werden, wird ein neues Array erstellt. Nun werden die im vorhandenen Array vorhandenen Elemente in ein neues Array kopiert und geben eine neue Slice-Referenz auf dieses Array (neues Array) zurück. Aus diesem Grund verdoppelt sich die Kapazität des Slice gegenüber der alten Kapazität (wie in Beispiel 1 gezeigt). Wenn der vorhandene Slice jedoch über genügend Kapazität verfügt, um neue Elemente aufzunehmen, wird kein neues Array erstellt und die Elemente werden im vorhandenen zugrunde liegenden Array gespeichert (wie in Beispiel 2 gezeigt).

Beispiel 1:

// Go program to illustrate the
// concept of appending slices.
package main
  
import (
    "fmt"
)
  
func main() {
  
    // Creating and initializing slice
    // Using shorthand declaration
    s1 := []int{234, 567, 7890, 1234, 234}
    s2 := []string{"Geeks", "For", "Geeks"}
  
    // Displaying slices with
    // their length and capacity
    fmt.Println("Slice_1: ", s1)
    fmt.Println("Length of Slice_1: ", len(s1))
    fmt.Println("Capacity of Slice_1: ", cap(s1))
    fmt.Println()
    fmt.Println("Slice_2: ", s2)
    fmt.Println("Length of Slice_2: ", len(s2))
    fmt.Println("Capacity of Slice_2: ", cap(s2))
  
    // Appending slices
    // Using append() function
    res1 := append(s1, 1000)
    res2 := append(s2, "GFG")
  
    // Displaying results
    fmt.Println()
    fmt.Println("New slice_1: ", res1)
    fmt.Println("New length of slice_1: ", len(res1))
    fmt.Println("New capacity of slice_1: ", cap(res1))
    fmt.Println()
    fmt.Println("New slice_2: ", res2)
    fmt.Println("New length of slice_2: ", len(res2))
    fmt.Println("New capacity of slice_2: ", cap(res2))
}

Ausgabe:

Slice_1:  [234 567 7890 1234 234]
Length of Slice_1:  5
Capacity of Slice_1:  5

Slice_2:  [Geeks For Geeks]
Length of Slice_2:  3
Capacity of Slice_2:  3

New slice_1:  [234 567 7890 1234 234 1000]
New length of slice_1:  6
New capacity of slice_1:  12

New slice_2:  [Geeks For Geeks GFG]
New length of slice_2:  4
New capacity of slice_2:  6

Beispiel 2:

// Go program to illustrate the
// concept of appending slices.
package main
  
import "fmt"
  
func main() {
  
    // Creating and initializing slice
    // Using make() function
    // Here 4 and 6 is the length
    // and capacity of the slice
    s1 := make([]int, 4, 6)
  
    // Copying the elements in the slice
    // Using copy() function
    copy(s1, []int{123, 456, 789, 977})
  
    // Displaying slice
    fmt.Println("Slice : ", s1)
    fmt.Println("Length: ", len(s1))
    fmt.Println("Capacity: ", cap(s1))
  
    // Appending slice
    // Using append() function
    s2 := append(s1, 3454, 678)
  
    // Displaying slice
    fmt.Println()
    fmt.Println("Slice : ", s2)
    fmt.Println("Length: ", len(s2))
    fmt.Println("Capacity: ", cap(s2))
  
}

Ausgabe:

Slice :  [123 456 789 977]
Length:  4
Capacity:  6

Slice :  [123 456 789 977 3454 678]
Length:  6
Capacity:  6

Anhängen an Null-Slice: Wie wir wissen, ist der Slice-Typ mit Nullwert Null und die Kapazität und Länge eines solchen Slice-Typs ist 0. Aber mit Hilfe der Append-Funktion ist es möglich, Werte an Null-Slice anzuhängen.

Beispiel:

// Go program to illustrate the
// concept of appending to nil slice.
package main
  
import "fmt"
  
func main() {
  
    // Creating nil slice
    var s1 []int
  
    // Displaying slice
    fmt.Println("Slice : ", s1)
    fmt.Println("Length: ", len(s1))
    fmt.Println("Capacity: ", cap(s1))
  
    // Appending to nil slice
    // Using append() function
    s2 := append(s1, 89, 45, 67, 23)
  
    // Displaying slice
    fmt.Println()
    fmt.Println("Slice : ", s2)
    fmt.Println("Length: ", len(s2))
    fmt.Println("Capacity: ", cap(s2))
  
}

Ausgabe:

Slice :  []
Length:  0
Capacity:  0

Slice :  [89 45 67 23]
Length:  4
Capacity:  4

Anhängen an ein anderes Slice mit …-Operator: Sie dürfen ein Slice mit Hilfe des -Operators an ein anderes Slice anhängen .

Beispiel:

// Go program to illustrate the concept 
// of appending to another slice.
package main
  
import "fmt"
  
func main() {
  
    // Creating and initializing slice
    // Using shorthand declaration
    s1 := []int{234, 567, 7890, 1234, 234}
    s2 := []int{10, 100, 1000, 10000}
  
    // Displaying slices with their
    // length and capacity
    fmt.Println("Slice_1: ", s1)
    fmt.Println("Length of Slice_1: ", len(s1))
    fmt.Println("Capacity of Slice_1: ", cap(s1))
      
    fmt.Println()
      
    fmt.Println("Slice_2: ", s2)
    fmt.Println("Length of Slice_2: ", len(s2))
    fmt.Println("Capacity of Slice_2: ", cap(s2))
  
    // Appending to another slice
    // Using append() function
    res1 := append(s1, s2...)
  
    // Displaying result
    fmt.Println()
    fmt.Println("New slice_1: ", res1)
    fmt.Println("New length of slice_1: ", len(res1))
    fmt.Println("New capacity of slice_1: ", cap(res1))
  
}

Ausgabe:

Slice_1:  [234 567 7890 1234 234]
Length of Slice_1:  5
Capacity of Slice_1:  5

Slice_2:  [10 100 1000 10000]
Length of Slice_2:  4
Capacity of Slice_2:  4

New slice_1:  [234 567 7890 1234 234 10 100 1000 10000]
New length of slice_1:  9
New capacity of slice_1:  12