Go · Stage 6 – Reusable Code · Lesson 40 of 64

Variadic Functions

Accept any number of values of one type.

Go code

func count(values ...int) int {
    return len(values)
}

fmt.Println(count(4, 5, 6))
Line-by-line explanation
  1. ...int collects integer arguments into a slice.
  2. Len returns the slice length.
  3. The call passes three arguments.
Expected output
3

This is the expected result.