此代码位于builti.go
:
// The append built-in function appends elements to the end of a slice. If
// it has sufficient capacity, the destination is resliced to accommodate the
// new elements. If it does not, a new underlying array will be allocated.
// Append returns the updated slice. It is therefore necessary to store the
// result of append, often in the variable holding the slice itself:
// slice = append(slice, elem1, elem2)
// slice = append(slice, anotherSlice...)
// As a special case, it is legal to append a string to a byte slice, like this:
// slice = append([]byte("hello "), "world"...)
func append(slice []Type, elems ...Type) []Type
最后一行让我感到非常困惑。我不知道...Type
的含义。
这些是其他代码:
package main
import "fmt"
func main() {
s := []int{1,2,3,4,5}
s1 := s[:2]
s2 := s[2:]
s3 := append(s1, s2...)
fmt.Println(s1, s2, s3)
}
结果是
[1 2] [3 4 5] [1 2 3 4 5]
我想...
的功能是从elems
中挑选所有元素,但我还没有找到官方解释。它是什么?
答案 0 :(得分:8)
builtin.go中的代码用作文档。代码未编译。
...
指定函数的最后一个参数是可变参数。变量参数为documented in the Go Language specification。
Type部分是任何Go类型的替身。