rest operator
# Node.js
function sum(...nums) {
let t = 0;
for (let n of nums) {
t += n;
}
return t;
}
const total = sum(1, 2, 3, 4, 5);
console.log(total);
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
Output
15
1
# Go
package main
import "fmt"
func sum(nums ...int) int {
var t int
for _, n := range nums {
t += n
}
return t
}
func main() {
total := sum(1, 2, 3, 4, 5)
fmt.Println(total)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Output
15
1
编辑 (opens new window)
上次更新: 2022/09/30, 11:34:22