Variadic Functions


Variadic Functions

A variadic function is a function that accepts a variable number of arguments. In Golang, it is possible to pass a varying number of arguments of the same type as referenced in the function signature. To declare a variadic function, the type of the final parameter is preceded by an ellipsis, "...", which shows that the function may be called with any number of arguments of this type. This type of function is useful when you don't know the number of arguments you are passing to the function, the best example is built-in Println function of the fmt package which is a variadic function.


Select single argument from all arguments of variadic function

This program demonstrates the use of a variadic function in Golang. A variadic function is a function that takes a variable number of arguments of a specific type.

In this program, the function variadicExample takes a variadic parameter of type string, indicated by the ... before the type name. This means that the function can accept any number of string arguments.

In the main function, we call variadicExample with four string arguments: "red", "blue", "green", and "yellow". These arguments are passed to the s parameter of the variadicExample function as a slice of strings.

Example

package main

import "fmt"

func main() {
	variadicExample("red", "blue", "green", "yellow")
}

func variadicExample(s ...string) {
	fmt.Println(s[0])
	fmt.Println(s[3])
}

Output

red
yellow

Because we are accessing the first and fourth elements of the s slice. Note that if we were to pass fewer than four arguments to variadicExample, the program would still run without error, but trying to access an index beyond the length of the slice would result in a runtime error.


Passing multiple string arguments to a variadic function

This program demonstrates the use of variadic function in Go, which allows a function to accept a variable number of arguments of the same type.

In this example, the function variadicExample() is defined to accept a variadic parameter of type string. This means that it can accept any number of string arguments. The main() function calls variadicExample() multiple times with different numbers of string arguments.

The first call to variadicExample() is made without any arguments, which is allowed since the function is defined to accept zero or more string arguments.

The second, third, and fourth calls pass different numbers of string arguments to variadicExample(). In each case, the function prints the contents of the s parameter using fmt.Println().

Example

package main

import "fmt"

func main() {
	
	variadicExample()
	variadicExample("red", "blue")
	variadicExample("red", "blue", "green")
	variadicExample("red", "blue", "green", "yellow")
}

func variadicExample(s ...string) {
	fmt.Println(s)
}

Output

[]
[red blue]
[red blue green]
[red blue green yellow]

The first call to variadicExample() prints an empty slice since no arguments were passed. The subsequent calls print the contents of the s parameter, which contains all the string arguments passed to the function.


Normal function parameter with variadic function parameter

Example

package main

import "fmt"

func main() {
	fmt.Println(calculation("Rectangle", 20, 30))
	fmt.Println(calculation("Square", 20))
}

func calculation(str string, y ...int) int {

	area := 1

	for _, val := range y {
		if str == "Rectangle" {
			area *= val
		} else if str == "Square" {
			area = val * val
		}
	}
	return area
}

Output

600
400

Pass different types of arguments in variadic function

In the following example, the function signature accepts an arbitrary number of arguments of type slice.

Example

package main

import (
	"fmt"
	"reflect"
)

func main() {
	variadicExample(1, "red", true, 10.5, []string{"foo", "bar", "baz"},
		map[string]int{"apple": 23, "tomato": 13})
}

func variadicExample(i ...interface{}) {
	for _, v := range i {
		fmt.Println(v, "--", reflect.ValueOf(v).Kind())
	}
}

Output

1 -- int
red -- string
true -- bool
10.5 -- float64
[foo bar baz] -- slice
map[apple:23 tomato:13] -- map