Advertisement
cwchen

[Go] Functions with varargs.

Sep 30th, 2017
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.24 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "log"
  5. )
  6.  
  7. func sum(args ...float64) float64 {
  8.     sum := 0.0
  9.  
  10.     for _, e := range args {
  11.         sum += e
  12.     }
  13.  
  14.     return sum
  15. }
  16.  
  17. func main() {
  18.     s := sum(1, 2, 3, 4, 5)
  19.  
  20.     if !(s == 15.0) {
  21.         log.Fatal("Wrong value")
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement