variadic function _ golang

Variadic functions can be called with any number of trailing arguments. For example, fmt.Println is a common variadic function

package main

import (
    "fmt"
)

func sum(nums ...int) {
    fmt.Println(nums, " ")
    total := 0
    for _, num := range nums {
        total += num
    }
    fmt.Println(total)
}

func main() {

    sum(1, 2)
    sum(1, 2, 3)

    nums := []int{1, 2, 3, 4}
    sum(nums...)
}
[1 2]  
3
[1 2 3]  
6
[1 2 3 4]  
10

总结 :

  1 : ....

posted on 2015-03-14 17:20  xjk112  阅读(140)  评论(0编辑  收藏  举报