Variadic Functions in Go
The function that called with the varying number of arguments is known as variadic function. Or in other words, a user is allowed to pass zero or more arguments in the variadic function. fmt.Printf is the example of the variadic function, it required one fixed argument at the starting after that it can accept any number of arguments.
Important Points:
- In the declaration of the variadic function, the type of the last parameter is preceded by an ellipsis, i.e, (…). It indicates that the function can be called at any number of parameters of this type.
Syntax:
function function_name(para1, para2...type)type{ // code... } - Inside the function …type behaves like a slice. For example, suppose we have a function signature, i.e, add( b…int)int, now the a parameter of type[]int.
- You can also pass an existing slice in a variadic function. To do this, we pass a slice of the complete array to the function as shown in Example 2 below.
- When you do not pass any argument in the variadic function, then the silce inside the function is nil.
- The variadic functions are generally used for string formatting.
- You can also pass multiple slice in the variadic function.
- You can not use variadic parameter as a return value, but you can return it as a slice.
Example 1:
// Go program to illustrate the // concept of variadic function package main import( "fmt" "strings") // Variadic function to join strings func joinstr(element...string)string{ return strings.Join(element, "-") } func main() { // zero argument fmt.Println(joinstr()) // multiple arguments fmt.Println(joinstr("GEEK", "GFG")) fmt.Println(joinstr("Geeks", "for", "Geeks")) fmt.Println(joinstr("G", "E", "E", "k", "S")) } |
Output:
GEEK-GFG Geeks-for-Geeks G-E-E-k-S
Example 2:
// Go program to illustrate the // concept of variadic function package main import( "fmt" "strings") // Variadic function to join strings func joinstr(element...string)string{ return strings.Join(element, "-") } func main() { // pass a slice in variadic function element:= []string{"geeks", "FOR", "geeks"} fmt.Println(joinstr(element...)) } |
Output:
geeks-FOR-geeks
When we use a Variadic function:
- Variadic function is used when you want to pass a slice in a function.
- Variadic function is used when we don’t know the quantity of parameters.
- When you use variadic function in your program, it increase the readability of your program.
Recommended Posts:
- Functions in Go Language
- Golang | Deadlock and Default Case in Select Statement
- Rune in Golang
- Encapsulation in Golang
- How to Map a Rune to the Specified Case in Golang?
- How to Map a Rune to Uppercase in Golang?
- How to Map a Rune to Title Case in Golang?
- How to Map a Rune to Lowercase in Golang?
- Check If the Rune is a Space Character or not in Golang
- Check If the Rune is a Symbolic Character or not in Golang
- Check If the Rune is a Lowercase Letter or not in Golang
- Check If the Rune is an Uppercase Letter or not in Golang
- Check If the Rune is a Decimal Digit or not in Golang
- Check If the Rune is a Letter or not in Golang
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



