首页 > 编程语言 > 详细

Go语言函数之可变参数

时间:2019-07-10 12:04:10      阅读:97      评论:0      收藏:0      [点我收藏+]
package main
//.... 加参数类型 
func Sum(nums ...int)int{

    total:=0
    for _,num:=range  nums{
        total+=num
    }
    return total
}
func main(){
    // Providing four arguments
    total :=Sum(1,2,3,4)
    println("The Sum is:",total)

    // Providing three arguments
    total = Sum(5, 7, 8)
    println("The Sum is:",total)

    nums:= []int{1,2,3,4,5} //slice
    total = Sum(nums...)   //通过...类似python中的解包
    println("The Sum is",total)

}

Go语言函数之可变参数

原文:https://www.cnblogs.com/c-x-a/p/11163036.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!