Variadic Sum
A variadic parameter accepts any number of arguments, including none. It is
written ...T and has to be the last parameter:
func Sum(nums ...int) int {
total := 0
for _, n := range nums {
total += n
}
return total
}
Sum() // 0
Sum(1, 2, 3) // 6
Inside the function nums is an ordinary []int. With no arguments it is
nil, which has length zero and ranges zero times, so the empty case usually
needs no code at all.
To pass a slice you already have, spread it with ...:
values := []int{1, 2, 3}
Sum(values...) // 6
Sum(values) // does not compile: []int is not an int
The two forms are not the same underneath. Sum(1, 2, 3) builds a fresh slice;
Sum(values...) passes values itself, so the parameter shares the caller's
backing array. A variadic function that writes to its parameter writes to the
caller's slice.
When you need "at least one", put a required parameter in front of the variadic:
func Largest(first int, rest ...int) int cannot be called with nothing, and the
compiler enforces it rather than a runtime check.
Task
Fill in the six functions in main.go.
Sum(nums ...int) intadds its arguments; no arguments is0.Largest(first int, rest ...int) intreturns the biggest value.SumAll(groups ...[]int) intadds every number in every group, by callingSumwith each group spread.Average(nums ...int) (float64, bool)returns the mean, andfalsewhen there are no arguments.Describe(format string, args ...any) stringforwards tofmt.Sprintf.SumEvens(nums ...int) intadds only the even arguments, leaving the caller's slice untouched.
Hints
Sum()needs noif. The parameter isnil,rangeovernilruns zero times, and the total stays0.SumAllis a variadic of slices. The outer call spreads groups in;Sum(group...)spreads each group back out.Averagedivides bylen(nums), which is why it has to check for zero first: integer or not, dividing by zero here is a panic.float64(Sum(nums...) / len(nums))is already wrong - it does the integer division and then converts the wrong answer. Convert both operands first.Describemust end inargs..., notargs. Passing the slice itself makes it one argument of type[]any, and%s has %dthen prints%!s(...)noise instead of the values.- The last test calls
SumEvens(nums...)and checksnumsafterwards. As long as you only read the parameter you are fine; a solution that sorts or compacts it in place will fail there and not in the sum.