Introduction
Variadic functions let us define functions that take an arbitrary number of arguments. This prevents us from coding against every possible variation of the input length. In Golang, it is viable to pass a varying number of arguments of a matching style as referenced in the function signature.
This function is best suited when you don't know the number of arguments you are passing to the function. The best sample is the built-in Println function of the fmt package, which is a variadic function.
Working of Variadic Functions
To declare a variadic function, an ellipsis "..." is given before mentioning the type of the final parameter. This displays that the function may be called with any number of arguments of this type. Let's make our own variadic function.
Example:
/*Package declaration*/
package main
import (
"fmt"
)
/* Func find(num int, nums ...int) in the line below takes a variable number of arguments for the nums parameter. */
func find(num int, nums ...int) {
fmt.Printf("the nums type is %T\n", nums)
found := false
/* The for loop goes through the nums slice. Prints the position of num if it's there in the slice. */
for i, v := range nums {
if v == num {
fmt.Println(num, " is found at index", i, "in", nums)
found = true
}
}
if !found {
fmt.Println(num, "is not found in ", nums)
}
fmt.Printf("\n")
}
func main() {
/* In the line below, the variable number of arguments to find the function are 89, 90, and 95. */
find(89, 89, 90, 95)
find(45, 56, 67, 45, 90, 109)
find(78, 38, 56, 98)
/* Here we see that the last find call have only one argument */
find(87)
}
Output:
the nums type is []int
89 is found at index 0 in [89 90 95]
the nums type is []int
45 is found at index 2 in [56 67 45 90 109]
the nums type is []int
78 is not found in [38 56 98]
the nums type is []int
87 is not found in []
Explanation:
In the program, as mentioned in the first comment above, func find(num int, nums ...int) takes in a variable number of arguments for the nums parameter. We see that inside the function find, the type of nums is []int, an integer slice.
Here we can see the way variadic functions work. It is by transforming the variable number of arguments to a slice of the kind of the variadic parameter. For example, following another comment in the find functions of the program,we see that the variable number of arguments to the find function are 89, 90, and 95. The find function anticipates a variadic int argument. Therefore these three arguments are changed by the compiler to a slice of type int []int{89, 90, 95}, and then they are handed to the find function.
Looking at the for loop, we see that it goes through the nums slice. Prints the position of num if it's there in the slice. If not there, it prints that the number is not found.
Within the above program, if we look at the last call of the find function, we see that it has only one argument. We have not passed or directed any argument to the variadic nums ...int parameter. As told earlier, this is completely legal, and in this case, nums will be a nil slice with a length and capacity of 0.