Go语言函数之可变参数

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)

}

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