Variadic Functions
Variadic functions can be called with any number of trailing arguments. They are useful when you need to work with an arbitrary number of parameters of the same type.
Key Points
- Variadic parameters allow functions to accept any number of arguments
- Declared with
...before the type:args ...T - Must be the last parameter in the function signature
- Inside the function, variadic parameters are treated as a slice
- Call with individual arguments or expand a slice with
slice... - Can be called with zero arguments (empty slice)
- Only one variadic parameter is allowed per function
- Common in standard library functions like
fmt.Printlnandappend
Basic Variadic Function
Use ... before the type to indicate a variadic parameter. The function receives a slice of that type:
package main
import "fmt"
// Variadic function that sums any number of ints
func sum(nums ...int) int {
total := 0
// nums is a slice within the function
for _, num := range nums {
total += num
}
return total
}
func main() {
// Call with varying numbers of arguments
fmt.Println(sum(1, 2))
fmt.Println(sum(1, 2, 3))
fmt.Println(sum(1, 2, 3, 4, 5))
// Can also call with no arguments
fmt.Println(sum())
}3
6
15
0Variadic with Slice Expansion
If you have a slice, you can pass it to a variadic function using ... syntax:
package main
import "fmt"
func sum(nums ...int) int {
total := 0
for _, num := range nums {
total += num
}
return total
}
func main() {
// Create a slice
numbers := []int{1, 2, 3, 4}
// Expand slice into variadic arguments
result := sum(numbers...)
fmt.Println("Sum of slice:", result)
// Can also mix individual args with slice
moreNumbers := []int{5, 6}
result = sum(moreNumbers...)
fmt.Println("Sum of more numbers:", result)
}Sum of slice: 10
Sum of more numbers: 11Variadic Function Patterns
| Pattern | Example | Description |
|---|---|---|
func(args ...T) | func sum(nums ...int) | Accept any number of T type args |
func(a T, args ...T) | func join(sep string, strs ...string) | Required param + variadic params |
func(args ...interface{}) | fmt.Println(a ...interface{}) | Accept any type of arguments |
func(args ...T) R | func max(nums ...int) int | Variadic with return value |
funcName(slice...) | sum(numbers...) | Expand slice to variadic args |
Variadic Function Characteristics
| Feature | Description | Example |
|---|---|---|
| Internal Type | Variadic params are received as a slice | nums ...int becomes []int |
| Position | Must be the last parameter | func(name string, nums ...int) |
| Zero Arguments | Can be called with no arguments | sum() works, nums will be empty slice |
| Slice Expansion | Slices can be expanded with ... | sum(slice...) |
| One Variadic | Only one variadic parameter allowed | Can't have func(a ...int, b ...string) |
Common Use Cases
| Use Case | Function | Description |
|---|---|---|
| Formatted Output | fmt.Println(a ...interface{}) | Print any number of values |
| String Joining | func join(sep string, strs ...string) | Concatenate multiple strings |
| Aggregation | func max(nums ...int) int | Find maximum of variable numbers |
| Append | append(slice, elems...) | Add multiple elements to slice |
| Error Formatting | fmt.Errorf(format, args ...interface{}) | Format error with multiple values |