What Are Variadic Functions in Go (... Syntax)

Variadic functions in Go use the ... syntax to accept a flexible number of arguments as a slice.

Variadic functions in Go accept a variable number of arguments by using the ... syntax in the parameter list, which the compiler treats as a slice of that type. You define the parameter with ...Type and access the arguments inside the function as a standard slice.

func sum(numbers ...int) int {
	total := 0
	for _, n := range numbers {
		total += n
	}
	return total
}

// Call with any number of integers
result := sum(1, 2, 3, 4, 5)