Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

[Author: @mdimamhosen Date: 2025-04-22 Category: interview-qa/Variadic Tags: [go, Variadic Function] ]

Variadic Functions in Go

A variadic function in Go is a function that can accept a variable number of arguments. You define a variadic function by placing ... before the type in the function signature, which allows the function to accept multiple arguments of that type.

Example Code: Variadic Function

package main

func print(number ...int) { // Variadic function to print numbers
	for _, n := range number {
		println(n) // Printing each number
	}
}

func main() {
	print(1, 2, 3, 4, 5) // Calling the variadic function with multiple arguments
	print(1, 2)           // Calling the variadic function with fewer arguments
	print(1)              // Calling the variadic function with a single argument
	print()               // Calling the variadic function with no arguments
}

Explanation:

  • print(number ...int) is a variadic function, meaning it can accept any number of int values.
  • In the main() function, different sets of arguments are passed to print():
    • A set of 5 integers
    • A set of 2 integers
    • A single integer
    • No arguments at all

More Explanation on Variadic Functions:

  • A variadic function works by collecting all the arguments passed into a slice.
  • You can pass a slice directly to a variadic function using ..., which unpacks the slice into individual arguments.

Example: Passing a Slice to a Variadic Function

package main

func print(numbers ...int) {
	for _, n := range numbers {
		println(n)
	}
}

func main() {
	nums := []int{10, 20, 30}
	print(nums...) // Unpacking a slice and passing to the variadic function
}

Interview Questions and Answers

1. What is the advantage of using variadic functions in Go?

Answer: Variadic functions allow for flexibility in function calls, enabling you to pass a varying number of arguments without needing to overload the function. This helps avoid writing multiple versions of the same function that differ only by the number of arguments they take.

2. Can a variadic function accept multiple types of arguments?

Answer: No, a variadic function in Go accepts only one type of argument. However, you can work around this limitation by using an interface{} type, which allows for any type, but you will lose type safety.

Code Example:

package main

import "fmt"

func printAnything(values ...interface{}) {
	for _, v := range values {
		fmt.Println(v)
	}
}

func main() {
	printAnything(1, "Hello", 3.14, true) // Accepting multiple types
}

3. Can you have multiple variadic parameters in a function?

Answer: No, Go only allows one variadic parameter in a function. You cannot have more than one variadic parameter in the function signature.

4. How do you handle a variadic function when no arguments are passed?

Answer: If no arguments are passed to a variadic function, the parameter inside the function will be an empty slice. This allows the function to still handle the case where no arguments are provided gracefully.

Code Example:

package main

func print(numbers ...int) {
	if len(numbers) == 0 {
		println("No numbers provided")
		return
	}
	for _, n := range numbers {
		println(n)
	}
}

func main() {
	print() // No arguments provided
}

5. Can you pass a slice to a variadic function?

Answer: Yes, you can pass a slice to a variadic function by using the ... syntax to unpack the slice.

Code Example:

package main

func print(numbers ...int) {
	for _, n := range numbers {
		println(n)
	}
}

func main() {
	nums := []int{10, 20, 30}
	print(nums...) // Passing a slice to a variadic function
}

This method unpacks the slice nums and passes its elements individually to the print function.