go 函数传参切片不确定个数

package main

import "fmt"

func calc(a , b int) (int,int){	
	sum := a+b
	sub := a-b
	return sum,sub
}

func calc1(a , b int) (sum int,sub int){	
	sum = a+b
	sub = a-b
	return
}

func calc_v1(b ...int) int{	
	sum := 0
	for i:=0;i<len(b);i++{
		sum += b[i]
	}
	return sum
}


func calc_v2(a int,b ...int) int{	
	sum := a
	for i:=0;i<len(b);i++{
		sum += b[i]
	}
	return sum
}


func main(){	
	// sayHello()
	// s := add(33,44 )
	// fmt.Println(s)

	// sum,sub := calc(33,44 )
	// fmt.Println(sum,sub)

	// sum,sub := calc1(33,44 )
	// fmt.Println(sum,sub)

	// sum,_ := calc1(33,44 )  // _ 忽略,不关注,返回值
	// fmt.Println(sum)

	sum :=calc_v1()
	//sum :=calc_v1(10)
	//sum :=calc_v1(10,20,30,40)
	fmt.Println(sum)

	//sum :=calc_v2(10,11,12)
	//sum :=calc_v1(10,20,40)
	//fmt.Println(sum)
}

posted @ 2022-04-29 23:41  ty1539  阅读(29)  评论(0编辑  收藏  举报