码迷,mamicode.com
首页 > 其他好文 > 详细

variadic function _ golang

时间:2015-03-14 18:16:59      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

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 : ....

variadic function _ golang

标签:

原文地址:http://www.cnblogs.com/jackkiexu/p/4337871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!