"... Type"的含义是什么?在围棋中? [英] What is the meaning of "...Type" in Go?

查看:72
本文介绍了"... Type"的含义是什么?在围棋中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在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的含义.

The last line made me feel very confused. I do not know the meaning of ...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中挑选所有元素,但是我还没有找到正式的解释.什么事?

I guess the function of ... is to pick all elements from elems, but I haven't found an official explanation. What is it?

推荐答案

builtin.go中的代码用作文档.该代码未编译.

The code in builtin.go serves as documentation. The code is not compiled.

...指定该函数的最终参数为可变参数.变体函数是Go语言规范中记录的 .简而言之,可变参数函数可以在最终参数中使用任意数量的参数来调用.

The ... specifies that the final parameter of the function is variadic. Variadic functions are documented in the Go Language specification. In short, variadic functions can be called with any number of arguments for the final parameter.

Type 部分是任何Go类型的替代.

The Type part is a stand-in for any Go type.

这篇关于"... Type"的含义是什么?在围棋中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆