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
- from __future__ import division
- def average(*numbers):
- return sum(numbers) / len(numbers)
- print average(1, 2, 3, 4) # 10/4 = 2.5
Go
- package main
- import "fmt"
- func average(numbers ...float64) float64 {
- total := 0.0
- for _, number := range numbers {
- total += number
- }
- return total / float64(len(numbers))
- }
- func main() {
- fmt.Println(average(1, 2, 3, 4)) // 2.5
- }