Variadic functions allow a function to accept a variable number of arguments of the same type. Instead of defining a fixed number of parameters, the ... syntax is used, making functions more flexible and suitable for scenarios where the number of inputs is unknown beforehand.
package main
import "fmt"
// Variadic function to calculate sum
func sum(nums ...int) int {
total := 0
for _, n := range nums {
total += n
}
return total
}
func main() {
fmt.Println("Sum of 1, 2, 3:", sum(1, 2, 3))
fmt.Println("Sum of 4, 5:", sum(4, 5))
fmt.Println("Sum of no numbers:", sum())
}
Output
Sum of 1, 2, 3: 6 Sum of 4, 5: 9 Sum of no numbers: 0
Syntax
func functionName(parameters ...Type) ReturnType {
// Code
}
Components:
- parameters '...'Type: indicates that the function can accept a variable number of arguments of type
Type. - You can access the arguments within the function as a slice.
Using Variadic Functions
Variadic arguments are automatically collected into a slice inside the function. For example:
func sum(nums ...int)
behaves similarly to:
func sum(nums []int)
However, when calling a variadic function, individual arguments can be passed directly without creating a slice manually. For example:
sum(10, 20, 30)
Passing a Slice to a Variadic Function
An existing slice can be expanded into individual arguments using '....'
package main
import "fmt"
func sum(nums ...int) int {
total := 0
for _, num := range nums {
total += num
}
return total
}
func main() {
numbers := []int{10, 20, 30}
fmt.Println(sum(numbers...))
}
Output
60
Explanation:
- numbers is a slice of integers.
- numbers... expands the slice into individual arguments.
- The function processes them as regular variadic arguments.
Calling a Variadic Function
You can call a variadic function with any number of arguments, including zero. The function treats the arguments as a slice.
package main
import "fmt"
func sum(nums ...int) int {
total := 0
for _, n := range nums {
total += n
}
return total
}
func main() {
fmt.Println("Sum of 1, 2, 3:", sum(1, 2, 3))
fmt.Println("Sum of 4, 5:", sum(4, 5))
fmt.Println("Sum of no numbers:", sum())
}
Output
Sum of 1, 2, 3: 6 Sum of 4, 5: 9 Sum of no numbers: 0
Variadic Functions with Other Parameters
You can also mix variadic parameters with regular parameters in a function. The variadic parameter must always be the last parameter.
package main
import "fmt"
func greet(greeting string, names ...string) {
for _, name := range names {
fmt.Println(greeting, name)
}
}
func main() {
greet("Hello", "Mike", "Liam")
greet("Welcome", "Jonathan")
}
Output
Hello Mike Hello Liam Welcome Jonathan
Explanation:
- greeting is a regular parameter.
- names is a variadic parameter.
- The for loop iterates through all names.
- The variadic parameter must always appear last in the function definition.
Limitations of Variadic Functions
- Variadic functions can only have one variadic parameter, and it must be the last parameter.
- You cannot have multiple variadic parameters in a single function definition. Example:
func add(nums ...int, name string) {}
This produces an error because the variadic parameter is not the last parameter.