October 2015
Beginner to intermediate
400 pages
14h 44m
English
A variadic function is one that can be called with varying
numbers of arguments.
The most familiar examples are fmt.Printf and its variants.
Printf requires one fixed argument at the beginning,
then accepts any number of subsequent arguments.
To declare a variadic function, the type of the final
parameter is preceded by an ellipsis, “...”, which
indicates that the function may be called with any number
of arguments of this type.
func sum(vals ...int) int {
total := 0
for _, val := range vals {
total += val
}
return total
}
The sum function above returns the sum of zero or more
int arguments.
Within the body of the function, the type of vals is
an []int slice.
When