Go语言函数之可变参数

mac2022-06-30  22

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

最新回复(0)