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)
Output
15
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)
}
Output
15
当前内容版权归 miguelmota 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 miguelmota .