📌  相关文章
📜  如何在 Golang 中附加一个切片?

📅  最后修改于: 2021-10-25 02:08:18             🧑  作者: Mango

在 Go 语言中切片比数组更强大、灵活、方便,是一种轻量级的数据结构。切片是一个可变长度的序列,它存储相似类型的元素,不允许在同一个切片中存储不同类型的元素。它就像一个具有索引值和长度的数组,但是切片的大小是调整大小的,它们不像数组那样是固定大小的。
正如我们已经知道切片是动态的,因此可以在 append()函数的帮助下将新元素附加到切片。此函数将新元素附加到切片的末尾。

句法:

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

这里,这个函数接受s切片, x…T意味着这个函数接受x参数的可变数量的参数。这种类型的函数也称为可变参数函数。

如果切片由数组支持并且数组是固定长度的,那么切片怎么可能是动态长度?

好吧,答案是当新元素附加到切片时,就会创建一个新数组。现在,现有数组中存在的元素被复制到一个新数组中,并返回对该数组(新数组)的新切片引用。因此,因此,切片的容量将是旧容量的两倍(如示例 1 所示)。但是如果现有切片有足够的容量来容纳新元素,则不会创建新数组,元素存储在现有的底层数组中(如示例 2 所示)。

示例 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))
}

输出:

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

示例 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))
  
}

输出:

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

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

追加到 nil 切片:我们知道零值切片类型是 nil,这种类型的切片的容量和长度都是 0。但是在 append函数的帮助下,可以将值附加到 nil 切片。

例子:

// 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))
  
}

输出:

Slice :  []
Length:  0
Capacity:  0

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

使用 …运算符附加到另一个切片:您可以在运算符的帮助下将一个切片附加到另一个切片

例子:

// 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))
  
}

输出:

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