Variadic Functions

In Python you can accept varying types with somefunction(*args) butthis is not possible with Go. You can however, make the type aninterface thus being able to get much more rich type structs.

Python

  1. from __future__ import division
  2.  
  3.  
  4. def average(*numbers):
  5. return sum(numbers) / len(numbers)
  6.  
  7. print average(1, 2, 3, 4) # 10/4 = 2.5

Go

  1. package main
  2.  
  3. import "fmt"
  4.  
  5. func average(numbers ...float64) float64 {
  6. total := 0.0
  7. for _, number := range numbers {
  8. total += number
  9. }
  10. return total / float64(len(numbers))
  11. }
  12.  
  13. func main() {
  14. fmt.Println(average(1, 2, 3, 4)) // 2.5
  15. }