Go Variadic Function
May 16, 2021 - 1 minute read
Example of Go variadic function:
func main() {
nums := []int{1, 2, 3}
// uses the existing slice
s1 := sum(nums...)
// creates a new slice
s2 := sum(1, 2, 3)
// Both s1 and s2 values are equal
// Variadic func can be called with no argument
s3 := sum()
}
func sum(nums ...int) (total int) {
for _, n := range nums {
total += n
}
}
The common example is the Println function from the fmt package.
func Println(a ...interface{}) (n int, err error)