Golang 如何追加一个slice
在Go语言中,slice比数组更强大、灵活、方便,是一种轻量级的数据结构。slice是一个可变长度的序列,用来存储相似类型的元素,你不允许在同一个slice中存储不同类型的元素。它就像一个有索引值和长度的数组一样,但片断的大小是可以调整的,它们不像数组那样有固定的大小。
我们已经知道,片断是动态的,所以新的元素可以在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 slice: 我们知道零值slice类型是nil,这种类型的slice的容量和长度是0。
例子
// 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
极客教程