This is a creation in Article, where the information may have evolved or changed.
Package <textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">Mainimport "FMT"//Here's a function that would take an arbitrary number//of ' ints ' as Arguments.func sum (nums ... int) {FMT. Print (Nums, "") Total: = 0for _, num: = Range Nums {total + = num}fmt. Println (total)}func main () {//Variadic functions can is called in the usual way//with individual arguments.sum (1, 2) sum ( 1, 2, 3)/If you already has multiple args in a slice,//apply them to a variadic function using//' func (Slice ...) ' Like this.nums: = []int{1, 2, 3, 4}sum (Nums ...)}</textarea>
| 12345678910111213141516171819202122232425262728 |
Package MainImport "FMT"//Here's a function that would take an arbitrary number//Of ' ints ' as arguments.funcsum(Nums ...int) {FMT.Print(Nums, " ") Total := 0 for _, Num := Range Nums { Total += Num}FMT.Println( Total)}funcMain() {//Variadic functions can called in the usual//with individual arguments.sum(1, 2)sum(1, 2, 3)//If You already has multiple args in a slice,//apply them to a variadic function using//' func (Slice ...) ' like this.Nums := []int{1, 2, 3, 4}sum(Nums...)} |
The value of the output:
<textarea wrap= "soft" class= "Crayon-plain print-no" data-settings= "DblClick" readonly style= "-MOZ-TAB-SIZE:4; -o-tab-size:4; -webkit-tab-size:4; Tab-size:4; FONT-SIZE:12PX!important; line-height:15px!important; " >[1 2] 3[1 2 3] 6[1 2 3 4] Ten </textarea>
| 123 |
< Span class= "Crayon-sy" >[ 1 2 3 [ 1 2 3 6 [ 1 2 3 4 10 |
the variable parameter function of Go