A logo showing the text blog.marcnuri.com
Español
Home»Go»Understanding Variadic Functions in Go

Recent Posts

  • Fabric8 Kubernetes Client 7.2 is now available!
  • Connecting to an MCP Server from JavaScript using AI SDK
  • Connecting to an MCP Server from JavaScript using LangChain.js
  • The Future of Developer Tools: Adapting to Machine-Based Developers
  • Connecting to a Model Context Protocol (MCP) Server from Java using LangChain4j

Categories

  • Artificial Intelligence
  • Front-end
  • Go
  • Industry and business
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • January 2017
  • December 2015
  • November 2015
  • December 2014
  • March 2014
  • February 2011
  • November 2008
  • June 2008
  • May 2008
  • April 2008
  • January 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

Understanding Variadic Functions in Go

2020-02-11 in Go tagged Go / Functions by Marc Nuri | Last updated: 2025-03-02
Versión en Español

Introduction

Variadic functions are a powerful feature in Go that allows functions to accept a variable number of arguments (from zero to many). This flexibility makes them particularly useful in scenarios where the number of inputs may vary. For example, the fmt.Println function is a variadic function that can accept any number of arguments of any type.

In this post, I'll explain what variadic functions are, how to define them, and how to use them effectively.

Defining variadic functions

To define a variadic function in Go, use an ellipsis (...) before the parameter type. This tells the compiler that the function can accept zero or more arguments of that type.

Here's an example:

main.go
package main

import (
	"fmt"
)

func greet(names ...string) {
	for _, name := range names {
		fmt.Printf("Greetings %s\n", name)
	}
}

func main() {
	greet()
	greet("blog.marcnuri.com")
	greet("Professor Falken", "Dr. Emmett Brown")
}

In this example, the greet function can take any number of string arguments.

In the main function, we call greet with zero, one, and two arguments. The output will be:

Greetings blog.marcnuri.com
Greetings Professor Falken
Greetings Dr. Emmett Brown
  • For the first call to greet, we pass no arguments, so the function prints nothing.
  • The second call with one argument prints a greeting to blog.marcnuri.com.
  • The third call with two arguments prints greetings to Professor Falken and Dr. Emmett Brown.

Tip

You can only have one variadic parameter in a function, and it must be the last parameter.

Using variadic functions

Variadic functions can be called with any number of arguments, including none. Inside the function, the variadic parameter behaves like a slice of the specified type. This allows for typical slice operations, such as iteration.

If you have a slice and want to pass its elements to a variadic function, use the ... operator to expand the slice:

main.go
package main

import (
	"fmt"
)

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

func main() {
	nums := []int{1, 2, 3, 4}
	fmt.Println(sum(nums...)) // Outputs: 10
}

Here, the sum function calculates the total of all provided integers. By passing nums..., we expand the slice into individual arguments for the variadic function.

Best practices and considerations

When using variadic functions, consider the following best practices:

  • Use sparingly: While variadic functions offer flexibility, they can make your code less clear if overused.
  • Type safety: Be cautious when using interface{} as the variadic parameter type, as it reduces type safety.
  • Performance: Variadic functions can be less efficient than fixed-arity functions in high-frequency operations, so use them judiciously.
  • Readability: Use variadic functions when they improve readability and maintainability by reducing the need for multiple function overloads.

Conclusion

Variadic functions in Go provide a powerful way to create flexible and readable code. They're particularly useful when you need to handle an unknown number of arguments or want to simplify your API. By understanding how to define and use variadic functions, you can write more elegant and efficient Go code.

Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation
How to Initialize a New Go Project with ModulesEnums in Go: An Alternative approach
© 2007 - 2025 Marc Nuri