4. Variadic

Many functions, especially those in the standard library, can take an arbitrary number of final arguments. This is accomplished by using the "..." syntax in the function signature.

A variadic function receives the variadic arguments as a slice.

concat.go
func concat(strs ...string) string {
    final := ""
    // strs is just a slice of strings
    for i := 0; i < len(strs); i++ {
        final += strs[i]
    }
    return final
}

func main() {
    final := concat("Hello ", "there ", "friend!")
    fmt.Println(final)
    // Output: Hello there friend!
}

The familiar fmt.Println()arrow-up-right and fmt.Sprintf()arrow-up-right are variadic! fmt.Println() prints each element with space delimitersarrow-up-right and a newline at the end.

func Println(a ...interface{}) (n int, err error)

Spread Operator

The spread operator allows us to pass a slice into a variadic function. The spread operator consists of three dots following the slice in the function call.

Assignment

We need to sum up the costs of all individual messages so we can send an end-of-month bill to our customers.

Complete the sum function to return the sum of all inputs.

Take note of how the variadic inputs and the spread operator are used in the test suite.

Solution

Go Variadic Functions

What Are Variadic Functions?

Variadic functions can accept any number of arguments of the same type. You define them using the ... syntax before the type in the function signature.

Key Concepts

Declaration Syntax

The ... before the type tells Go "this parameter can receive zero or more arguments of this type."

Inside the Function

The variadic parameter behaves like a slice. You can:

  • Loop through it

  • Check its length with len()

  • Access elements by index

  • Use any slice operations

Calling Variadic Functions

You can pass any number of arguments (including zero):

The Spread Operator (...)

When you already have a slice and want to pass it to a variadic function, use the spread operator by adding ... after the slice name.

Without the spread operator, you'd get a compile error because you're trying to pass a slice where individual integers are expected.

Common Standard Library Examples

Many Go standard library functions are variadic:

Your Solution Explained

1

Accept variadic ints

nums ...int β€” the function accepts any number of integers, which are available inside the function as a slice named nums.

2

Initialize accumulator

totalCost := 0 β€” create a variable to accumulate the sum.

3

Iterate inputs

Loop through the nums slice and add each value to totalCost.

4

Return result

Return totalCost as the final sum.

Alternative using range (more idiomatic Go):

Rules and Constraints

  • Variadic parameter must be the last parameter in the function signature.

  • You can only have one variadic parameter per function.

  • Mix regular and variadic parameters:

Summary

Concept
Syntax
Purpose

Define variadic function

func name(param ...type)

Accept multiple arguments

Use inside function

param[i] or range param

Treat as a slice

Spread operator

functionName(slice...)

Pass an existing slice into a variadic function