The Variadic function supports any number of incoming parameters. For example: FMT. PRINTLN is a common variable-parameter function.
Package Mainimport "FMT"//This function can use any number of int types as parameter func sum (nums ... int) { fmt. Print (Nums, "") total: = 0 for _, num: = range Nums {Total + = num } fmt. Println (total)}func main () { //Variadic function support is used in a regular manner, //also supports only one sum (1, 2) sum (1, 2, 3) // If you need an incoming parameter in a slice, like the following //"func (Slice ...)" Break the slices into the nums: = []int{1, 2, 3, 4} sum (Nums ...)}
The output is:
<strong>$ Go run variadic-functions.go </strong>[1 2] 3[1 2 3] 6[1 2 3 4] 10
Another important feature of functions in the go language is the construction of function closures. We will explain in the next section.
Next example: Go by Example:closures.
English original
Go by Example:variadic Functions