A Definitive Guide to Variadic Functions in Golang

In this class, we follow up our discussions on functions by focusing on variadic functions. By the end of the class, you’ll have learnt how to implement variadic functions in Golang.

Now, you might be thinking, “This is great, but why should I use variadic functions in Golang?”

Simple. Variadic functions—functions that accept variable numbers of arguments or parameters—can help you to create type-safety programs. For instance, you can create a function that sums up numbers or concatenates strings in a logical manner where you can use any number of operands.

The variadic functions can take a list of arguments, and call the function name only once—with the parameters that have been given in that list—therefore, helping to pass variable numbers of arguments in that function. That’s why these functions create type-safety programs.

So, are you ready to learn how to implement these functions?

To start us off, we should figure out a variadic function as a function that has variable numbers of arguments. Here’s an illustration:

a: = AddAll (100, 2000, 4500)
b := AddAll (3400, 50590, 40490, 73030, 45002, 5600)
c := AddAll (100010, -24040)

In the example above the variadic function, “AddAll” is being called by different counts of parameters lists. The first, second and third have three, six and two counts respectively.

Right now, you must be familiar with variations of the “fmt” functions. For instance, the fmt.Print (), the fmt.Printf () and the fmt.Println () have different implementations in Golang. These are examples of functions which are variadic in Golang. You can also create your own variadic functions and use them.

How about defining your own variadic functions?
The most important consideration to note in defining your own variadic functions is learning how to declare the parameters in Golang. For you to declare your parameters, here’s what you should know: use the “…” before the data type name of the last argument to specify whether that function takes zero or more parameters.

For instance, here’s how the fmt.Println function is implemented in Golang:

func Println (a ...interface { }) (a int, err error)

In the above example, the Println function can take any number of values that are of any data type. You can also pass the segments of integers “int” by following the code segment with the “…:”
Here’s an example:

func main() {
a := [ ]int{1,2,3}
fmt.Println (add (a...))
}

The above Golang code takes zero or more arguments—which in this case are zero or more integers. You can invoke the Println function like any other functions. However, when you invoke such a function, you should pass as many integers as possible in your variadic function.
For instance, here’s an example that takes random number of integers as arguments:

func add (numbers ...int) {
fmt.Print (numbers, " ")
sum := 100
for _, numbers:= range numbers {
sum += numbers
}
fmt.Println(sum)
}

How do we call variadic functions?
Well, for you to call a variadic function in your main function, you’ve to know the number of arguments that you have used. Your variadic function will be called in a typical way with the individual arguments. However, if you already have multiple parameters in your slice, then you should apply them to your variadic function using the function (slice…). Here’s an example:

package main
import "fmt"
func add (numbers ...int) {
fmt.Print (numbers, " ")
sum := 100
for _, numbers:= range numbers {
sum += numbers
}
fmt.Println (sum)
}
func main() {
add (3839, 200)
add (3003, 5050, 7400)
numbers: = []int{1000, 3003, 5050, 6006}
add (numbers...)
}

There you have it—everything that you should know about variadic functions. Use this knowledge to create more programs that use variadic functions.

GET YOUR FREE Golang EBOOK!

(Visited 4,907 times, 1 visits today)

Comments