Variadic functions
Context
Section titled “Context”Variadic functions accept a variable number of arguments. The last parameter is declared with ...T, and inside the function it behaves as a slice of type []T.
Example
Section titled “Example”Write a variadic function that sums any number of integers.
Code example
Section titled “Code example”package main
import "fmt"
func sum(nums ...int) int { total := 0 for _, n := range nums { total += n } return total}
func main() { fmt.Println(sum(1, 2, 3)) fmt.Println(sum(4, 5, 6, 7))
// Passing a slice to a variadic function nums := []int{10, 20, 30} fmt.Println(sum(nums...))}Output
Section titled “Output”62260