Go Gopher How to Go

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.Println and append

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())
}
Output:
3
6
15
0

Variadic 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)
}
Output:
Sum of slice: 10
Sum of more numbers: 11

Variadic Function Patterns

PatternExampleDescription
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) Rfunc max(nums ...int) intVariadic with return value
funcName(slice...)sum(numbers...)Expand slice to variadic args

Variadic Function Characteristics

FeatureDescriptionExample
Internal TypeVariadic params are received as a slicenums ...int becomes []int
PositionMust be the last parameterfunc(name string, nums ...int)
Zero ArgumentsCan be called with no argumentssum() works, nums will be empty slice
Slice ExpansionSlices can be expanded with ...sum(slice...)
One VariadicOnly one variadic parameter allowedCan't have func(a ...int, b ...string)

Common Use Cases

Use CaseFunctionDescription
Formatted Outputfmt.Println(a ...interface{})Print any number of values
String Joiningfunc join(sep string, strs ...string)Concatenate multiple strings
Aggregationfunc max(nums ...int) intFind maximum of variable numbers
Appendappend(slice, elems...)Add multiple elements to slice
Error Formattingfmt.Errorf(format, args ...interface{})Format error with multiple values