A variadic function in Go accepts zero or more arguments for its final parameter. You declare it by putting ... before the parameter type, for example func sum(nums ...int) int, and inside the function the parameter behaves as an ordinary slice ([]int).
Go uses variadic functions in the standard library all the time: fmt.Println, append and errors.Join are all variadic. They let you write flexible APIs without forcing callers to build a slice first.
The variadic parameter must be the last one in the parameter list, and a function can have only one.
Inside sum, numbers has type []int. When the caller passes no values, numbers is nil, so len(numbers) is 0 and ranging over it is safe.
To pass a slice as the variadic arguments, add ... after it in the call:
You cannot mix individual values and a spread slice in the same call. sum(0, nums...) does not compile. Build a new slice first if you need that.
When you call f(nums...), the function receives the same backing array, with no copy. If the function modifies elements, the caller sees the change:
When you pass individual values instead, Go creates a fresh slice for the call, so the modification stays local.
Fixed parameters come first. This is the pattern used by fmt.Printf and by most logging helpers.
Notice args... forwards the variadic values to fmt.Sprintf. Forgetting the trailing ... would pass the whole slice as a single argument, a very common bug.
A parameter of type ...any (the modern spelling of ...interface{}) accepts values of different types. To pass a []string you must convert it to []any first, because Go does not convert slice types implicitly:
Prefer generics when you want type safety. For example:
Declaring first separately also forces callers to pass at least one value.
Variadic parameters power the functional options pattern, which keeps constructors stable while allowing new settings:
Callers can pass no options, one option or many, and you can add new options later without breaking existing code.
func(...int) int; you can assign it to a variable of that type but cannot call it with a slice without ....nil slice with ... is legal and gives a nil parameter...., since the caller may modify it later.It is a slice of the declared type. args ...string is a []string inside the function body.
No. Only the final parameter may be variadic, which keeps argument matching unambiguous.
fmt.Println(slice) print a list but fmt.Println(slice...) fails?fmt.Println takes ...any. A []string is not a []any, so slice... does not compile. Passing slice without the spread treats it as one argument and prints it as a list.
Calling with individual values allocates a small slice unless the compiler can keep it on the stack, which it often can. The cost is rarely significant outside hot loops.
Variadic functions take ...Type as their last parameter and see it as a slice. Call them with any number of values, or pass an existing slice using slice.... Remember that the spread form shares the backing array, forward arguments with args..., and use the pattern for APIs such as logging helpers and functional options.