rest operator


Node.js

  1. function sum(...nums) {
  2. let t = 0
  3. for (let n of nums) {
  4. t += n
  5. }
  6. return t
  7. }
  8. const total = sum(1, 2, 3, 4, 5)
  9. console.log(total)

Output

  1. 15

Go

  1. package main
  2. import "fmt"
  3. func sum(nums ...int) int {
  4. var t int
  5. for _, n := range nums {
  6. t += n
  7. }
  8. return t
  9. }
  10. func main() {
  11. total := sum(1, 2, 3, 4, 5)
  12. fmt.Println(total)
  13. }

Output

  1. 15