概述
在golang中,数组的大小是其类型的一部分。这就是为什么数组的长度在创建时是固定的,以后不能改变。 这就是slice出现的地方。Slice比数组更强大,使用起来更方便。 事实上,Slice更类似于另一种编程语言中的数组。在本教程中,我们将研究如何向一个片断添加或追加。
在内部,一个片断由三样东西表示。
-
指向底层数组的指针
-
底层数组的当前长度
-
总容量,这是底层数组可以扩展的最大容量。
在这里阅读更多关于slice的信息- https://golangbyexample.com/slice-in-golang/
append() 函数
go内置包提供了一个append函数,可以用来在片尾追加或添加到片中。下面是这个函数的签名
func append(slice []Type, elems ...Type) []Type
第一个参数是分片本身。第二个参数是变量的数量,即
elems ...Type
'...'操作符是变量语法。所以基本上...Type意味着append函数可以接受可变数量的Type类型的参数。下面是使用这个函数的方法。在下面的代码中,我们要向一个有两个元素的片断追加4。它在最后追加,并返回原始片断。这就是为什么我们要在数字变量中再次收集结果。将结果分配给其他变量也是可以的。
numbers := []int{1,2}
numbers := append(numbers, 4) //Slice will become [1, 2, 3, 4]
附加任何数量的元素也是可以的,因为第二个参数是变量参数。
numbers := []int{1,2}
numbers := append(numbers, 3, 4, 5) //Slice will become [1, 2, 3, 4, 5]
这个函数在后台增加了切片的长度和容量。有两种情况
-
当片断长度小于容量时
-
当片断长度等于容量时
当片断长度小于容量时
在这种情况下,通过使用append函数,切片的长度将增加一个,而其容量没有任何变化。让我们看一个例子
package main
import "fmt"
func main() {
numbers := make([]int, 3, 5)
numbers[0] = 1
numbers[1] = 2
numbers[2] = 3
fmt.Printf("numbers=%v\n", numbers)
fmt.Printf("length=%d\n", len(numbers))
fmt.Printf("capacity=%d\n", cap(numbers))
//Append number 4
numbers = append(numbers, 4)
fmt.Println("\nAppend Number 4")
fmt.Printf("numbers=%v\n", numbers)
fmt.Printf("length=%d\n", len(numbers))
fmt.Printf("capacity=%d\n", cap(numbers))
//Append number 5
numbers = append(numbers, 4)
fmt.Println("\nAppend Number 5")
fmt.Printf("numbers=%v\n", numbers)
fmt.Printf("length=%d\n", len(numbers))
fmt.Printf("capacity=%d\n", cap(numbers))
}
输出
numbers=[1 2 3]
length=3
capacity=5
Append Number 4
numbers=[1 2 3 4]
length=4
capacity=5
Append Number 5
numbers=[1 2 3 4 4]
length=5
capacity=5
所有情况下的容量都没有变化,它是5,而长度增加了1。
当分片长度等于容量时
在这种情况下,由于没有更多的容量,所以没有新的元素可以被容纳。 所以在这种情况下,一个两倍于容量的数组将被分配。当前由分片指向的数组将被复制到这个新的数组中。现在分片将开始指向这个新数组。因此,容量将增加一倍,长度将增加1。 让我们看一个例子
package main
import "fmt"
func main() {
numbers := make([]int, 3, 3)
numbers[0] = 1
numbers[1] = 2
numbers[2] = 3
fmt.Printf("numbers=%v\n", numbers)
fmt.Printf("length=%d\n", len(numbers))
fmt.Printf("capacity=%d\n", cap(numbers))
//Append number 4
numbers = append(numbers, 4)
fmt.Println("\nAppend Number 4")
fmt.Printf("numbers=%v\n", numbers)
fmt.Printf("length=%d\n", len(numbers))
fmt.Printf("capacity=%d\n", cap(numbers))
}
输出
numbers=[1 2 3]
length=3
capacity=3
Append Number 4
numbers=[1 2 3 4]
length=4
capacity=6
注意在上面的例子中,容量增加了一倍。
也可以将一个片断附加到另一个片断上。下面是它的格式。
res := append(slice1, slice2...)
注意在第二个片断后面的**'...'**。 '...' 是一个操作符,意味着参数是一个变量参数。意味着在运行期间slice2将被扩展为其单独的元素,这些元素被作为多个参数传递给append函数。
package main
import "fmt"
func main() {
numbers1 := []int{1, 2}
numbers2 := []int{3, 4}
numbers := append(numbers1, numbers2...)
fmt.Printf("numbers=%v\n", numbers)
fmt.Printf("length=%d\n", len(numbers))
fmt.Printf("capacity=%d\n", cap(numbers))
}
输出
numbers=[1 2 3 4]
length=4
capacity=4
字符串的append() 函数
Go中的字符串只不过是一个字节序列。因此,将一个字符串追加到一个字节片中是合法的。下面是这方面的程序。请注意字符串末尾的**'...'**。
package main
import "fmt"
func main() {
sample := "Hello"
suffix := "World"
result := append([]byte(sample), suffix...)
fmt.Printf("sample: %s\n", string(result))
}
输出
sample: HelloWorld