Quidest?

Variadic Functions in Go

ยท Lorenzo Drumond

Some GO functions can take a variable number of parameters: these are called variadic functions.

To define a variadic function in Go, we need to use the ... symbol, followed by a type.

The ... indicates that there can be zero, one, two or any number of parameters of that type to the function:

1func AddMany(inputs ...float64) float64 {
2  var total float64
3  for _, val := range inputs {
4    total += val
5  }
6
7  return total
8}

Inside the function body, inputs acts like a slice.

References

#parameters #body #signature #functions #slice #type #variadic #golang #tuple #multiple #list #expression #declare #programming #arguments