# Golang Variadic Functions With Example

Rate this post

You must have heard about variadic functions in Golang. They are one of the most powerful features of Golang, and they can make your code more flexible and concise. In this blog, we’ll discuss Golang variadic arguments, their usage, and best practices.

## What are Variadic Functions in Golang?

A variadic function is a function that can accept an arbitrary number of arguments. In Golang, you can define a variadic function by adding an ellipsis (three dots) before the last parameter of the function. For example:

``````func myFunc(arg1 int, arg2 ...string) {
// function body
}``````

In this example, `arg2` is a variadic argument, which means it can accept zero or more string arguments.

## How to Use Variadic Function in Go?

Variadic function can be useful in many situations, such as when you want to pass a variable number of arguments to a function. Let’s look at a few examples:

Example 1: Finding the Sum of Numbers

Suppose you want to find the sum of a variable number of integers. You can use a variadic function to achieve this:

``````func sum(numbers ...int) int {
total := 0
for _, num := range numbers {
total += num
}
}``````

In this example, `numbers` is a variadic argument, which means it can accept zero or more integers. The function iterates over the `numbers` slice using a `for` loop and adds each element to the `total` variable. Finally, the function returns the `total`.

You can call this function with any number of integers:

``````fmt.Println(sum(1, 2, 3)) // Output: 6
fmt.Println(sum(1, 2, 3, 4, 5)) // Output: 15
fmt.Println(sum()) // Output: 0``````

Example 2: Formatting Strings

The `fmt.Printf` function is a variadic function that is used to format strings. Here’s an example:

``fmt.Printf("My name is %s and I'm %d years old\n", "John", 30)``

In this example, the first argument is a string that contains two placeholders (`%s` and `%d`) for the second and third arguments. The `fmt.Printf` function will replace the placeholders with the corresponding values.

## Go Variadic Functions Best Practices

Now that you know how to use variadic functions in Golang, let’s discuss some best practices:

1. Keep the number of arguments reasonable: Variadic functions are useful for accepting a variable number of arguments, but it’s not a good idea to accept too many arguments. It can make your code difficult to read and maintain. Try to limit the number of arguments to a reasonable number.
2. Use variadic functions for similar types: It’s a good practice to use variadic functions for accepting arguments of similar types. For example, in the `sum` function example, we’re accepting integers. If you mix different types of arguments, it can make your code more complicated and error-prone.
3. Use named return values: When you’re using variadic functions, it’s a good practice to use named return values. This can make your code more readable and less error-prone. Here’s an example:
``````func sum(numbers ...int) (total int) {
for _, num := range numbers {
total += num
}
return
}``````

In this example, we’re using a named return value (`total`). This means that the `total` variable will be initialized to 0 automatically. We’re also using the `return` keyword without any arguments, which means that we’re returning the named return value (`total`).

1. Don’t use variadic functions for everything: While variadic functions can be useful, they’re not always the best choice. Sometimes it’s better to use regular functions with a fixed number of arguments. Use your judgement and decide whether a variadic function is the best option for your use case.
2. Document your variadic functions: When you’re using variadic functions, it’s important to document them properly. Explain what the function does, what arguments it accepts, and what the return value is. This can make your code more understandable for other developers who may use your code in the future.