You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
349 B
25 lines
349 B
3 years ago
|
package main
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
func main() {
|
||
|
nums := []int {1, 2, 3, 5, 4}
|
||
|
sum := 0
|
||
|
for _, num := range nums{
|
||
|
//fmt.Println(num)
|
||
|
sum += num
|
||
|
}
|
||
|
fmt.Println(sum)
|
||
|
fmt.Println(mysum(1,2,3))
|
||
|
fmt.Println(mysum(1,2,3,4))
|
||
|
}
|
||
|
|
||
|
// 可变参数函数
|
||
|
func mysum(nums ...int) int {
|
||
|
total := 0
|
||
|
for _, num := range nums{
|
||
|
total += num
|
||
|
}
|
||
|
return total
|
||
|
}
|