Variadic functions in Go (practical use cases)
- Accept an arbitrary number of arguments
- Simulate optional arguments
Here are some examples of variadic function in the Go standard library
- variadic argument has to be the last one
- Clean and elegant API
- Generic and ambiguous arguments and argument names
- No automatic conversion for
[]interface{}
aka[]any
Returning the passed slice You can’t use a variadic param as a result type, but, you can return it as a slice.
func f(nums ...int) []int { nums[1] = 10 return nums }