How To Use Variadic Functions in Go

Written by Gopher Guides

A variadic function is a function that accepts zero, one, or more values as a single argument. While variadic functions are not the common case, they can be used to make your code cleaner and more readable.

Variadic functions are more common than they seem. The most common one is the Println function from the fmt package.

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

A function with a parameter that is preceded with a set of ellipses (...) is considered a variadic function. The ellipsis means that the parameter provided can be zero, one, or more values. For the fmt.Println package, it is stating that the parameter a is variadic.

Let’s create a program that uses the fmt.Println function and pass in zero, one, or more values:

print.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. fmt.Println()
  5. fmt.Println("one")
  6. fmt.Println("one", "two")
  7. fmt.Println("one", "two", "three")
  8. }

The first time we call fmt.Println, we don’t pass any arguments. The second time we call fmt.Println we pass in only a single argument, with the value of one. Then we pass one and two, and finally one, two, and three.

Let’s run the program with the following command:

  1. go run print.go

We’ll see the following output:

Output

  1. one
  2. one two
  3. one two three

The first line of the output is blank. This is because we didn’t pass any arguments the first time fmt.Println was called. The second time the value of one was printed. Then one and two, and finally one, two, and three.

Now that we have seen how to call a variadic function, let’s look at how we can define our own variadic function.

Defining a Variadic Function

We can define a variadic function by using an ellipsis (...) in front of the argument. Let’s create a program that greets people when their names are sent to the function:

hello.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. sayHello()
  5. sayHello("Sammy")
  6. sayHello("Sammy", "Jessica", "Drew", "Jamie")
  7. }
  8. func sayHello(names ...string) {
  9. for _, n := range names {
  10. fmt.Printf("Hello %s\n", n)
  11. }
  12. }

We created a sayHello function that takes only a single parameter called names. The parameter is variadic, as we put an ellipsis (...) before the data type: ...string. This tells Go that the function can accept zero, one, or many arguments.

The sayHello function receives the names parameter as a slice. Since the data type is a string, the names parameter can be treated just like a slice of strings ([]string) inside the function body. We can create a loop with the range operator and iterate through the slice of strings.

If we run the program, we will get the following output:

Output

  1. Hello Sammy
  2. Hello Sammy
  3. Hello Jessica
  4. Hello Drew
  5. Hello Jamie

Notice that nothing printed for the first time we called sayHello. This is because the variadic parameter was an empty slice of string. Since we are looping through the slice, there is nothing to iterate through, and fmt.Printf is never called.

Let’s modify the program to detect that no values were sent in:

hello.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. sayHello()
  5. sayHello("Sammy")
  6. sayHello("Sammy", "Jessica", "Drew", "Jamie")
  7. }
  8. func sayHello(names ...string) {
  9. if len(names) == 0 {
  10. fmt.Println("nobody to greet")
  11. return
  12. }
  13. for _, n := range names {
  14. fmt.Printf("Hello %s\n", n)
  15. }
  16. }

Now, by using an if statement, if no values are passed, the length of names will be 0, and we will print out nobody to greet:

Output

  1. nobody to greet
  2. Hello Sammy
  3. Hello Sammy
  4. Hello Jessica
  5. Hello Drew
  6. Hello Jamie

Using a variadic parameter can make your code more readable. Let’s create a function that joins words together with a specified delimiter. We’ll create this program without a variadic function first to show how it would read:

join.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. var line string
  5. line = join(",", []string{"Sammy", "Jessica", "Drew", "Jamie"})
  6. fmt.Println(line)
  7. line = join(",", []string{"Sammy", "Jessica"})
  8. fmt.Println(line)
  9. line = join(",", []string{"Sammy"})
  10. fmt.Println(line)
  11. }
  12. func join(del string, values []string) string {
  13. var line string
  14. for i, v := range values {
  15. line = line + v
  16. if i != len(values)-1 {
  17. line = line + del
  18. }
  19. }
  20. return line
  21. }

In this program, we are passing a comma (,) as the delimiter to the join function. Then we are passing a slice of values to join. Here is the output:

Output

  1. Sammy,Jessica,Drew,Jamie
  2. Sammy,Jessica
  3. Sammy

Because the function takes a slice of string as the values parameter, we had to wrap all of our words in a slice when we called the join function. This can make the code difficult to read.

Now, let’s write the same function, but we’ll use a variadic function:

join.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. var line string
  5. line = join(",", "Sammy", "Jessica", "Drew", "Jamie")
  6. fmt.Println(line)
  7. line = join(",", "Sammy", "Jessica")
  8. fmt.Println(line)
  9. line = join(",", "Sammy")
  10. fmt.Println(line)
  11. }
  12. func join(del string, values ...string) string {
  13. var line string
  14. for i, v := range values {
  15. line = line + v
  16. if i != len(values)-1 {
  17. line = line + del
  18. }
  19. }
  20. return line
  21. }

If we run the program, we can see that we get the same output as the previous program:

Output

  1. Sammy,Jessica,Drew,Jamie
  2. Sammy,Jessica
  3. Sammy

While both versions of the join function do the exact same thing programmatically, the variadic version of the function is much easier to read when it is being called.

Variadic Argument Order

You can only have one variadic parameter in a function, and it must be the last parameter defined in the function. Defining parameters in a variadic function in any order other than the last parameter will result in a compilation error:

join.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. var line string
  5. line = join(",", "Sammy", "Jessica", "Drew", "Jamie")
  6. fmt.Println(line)
  7. line = join(",", "Sammy", "Jessica")
  8. fmt.Println(line)
  9. line = join(",", "Sammy")
  10. fmt.Println(line)
  11. }
  12. func join(values ...string, del string) string {
  13. var line string
  14. for i, v := range values {
  15. line = line + v
  16. if i != len(values)-1 {
  17. line = line + del
  18. }
  19. }
  20. return line
  21. }

This time we put the values parameter first in the join function. This will cause the following compilation error:

Output

  1. ./join_error.go:18:11: syntax error: cannot use ... with non-final parameter values

When defining any variadic function, only the last parameter can be variadic.

Exploding Arguments

So far, we have seen that we can pass zero, one, or more values to a variadic function. However, there will be occasions when we have a slice of values and we want to send them to a variadic function.

Let’s look at our join function from the last section to see what happens:

join.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. var line string
  5. names := []string{"Sammy", "Jessica", "Drew", "Jamie"}
  6. line = join(",", names)
  7. fmt.Println(line)
  8. }
  9. func join(del string, values ...string) string {
  10. var line string
  11. for i, v := range values {
  12. line = line + v
  13. if i != len(values)-1 {
  14. line = line + del
  15. }
  16. }
  17. return line
  18. }

If we run this program, we will receive a compilation error:

Output

  1. ./join-error.go:10:14: cannot use names (type []string) as type string in argument to join

Even though the variadic function will convert the parameter of values ...string to a slice of strings []string, we can’t pass a slice of strings as the argument. This is because the compiler expects discrete arguments of strings.

To work around this, we can explode a slice by suffixing it with a set of ellipses (...) and turning it into discrete arguments that will be passed to a variadic function:

join.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. var line string
  5. names := []string{"Sammy", "Jessica", "Drew", "Jamie"}
  6. line = join(",", names...)
  7. fmt.Println(line)
  8. }
  9. func join(del string, values ...string) string {
  10. var line string
  11. for i, v := range values {
  12. line = line + v
  13. if i != len(values)-1 {
  14. line = line + del
  15. }
  16. }
  17. return line
  18. }

This time, when we called the join function, we exploded the names slice by appending ellipses (...).

This allows the program to now run as expected:

Output

  1. Sammy,Jessica,Drew,Jamie

It’s important to note that we can still pass a zero, one, or more arguments, as well as a slice that we explode. Here is the code passing all the variations that we have seen so far:

join.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. var line string
  5. line = join(",", []string{"Sammy", "Jessica", "Drew", "Jamie"}...)
  6. fmt.Println(line)
  7. line = join(",", "Sammy", "Jessica", "Drew", "Jamie")
  8. fmt.Println(line)
  9. line = join(",", "Sammy", "Jessica")
  10. fmt.Println(line)
  11. line = join(",", "Sammy")
  12. fmt.Println(line)
  13. }
  14. func join(del string, values ...string) string {
  15. var line string
  16. for i, v := range values {
  17. line = line + v
  18. if i != len(values)-1 {
  19. line = line + del
  20. }
  21. }
  22. return line
  23. }

Output

  1. Sammy,Jessica,Drew,Jamie
  2. Sammy,Jessica,Drew,Jamie
  3. Sammy,Jessica
  4. Sammy

We now know how to pass zero, one, or many arguments, as well as a slice that we explode, to a variadic function.

Conclusion

In this tutorial, we have seen how variadic functions can make your code cleaner. While you won’t always need to use them, you may find them useful:

  • If you find that you’re creating a temporary slice just to pass to a function.
  • When the number of input params are unknown or will vary when called.
  • To make your code more readable.

To learn more about creating and calling functions, you can read How to Define and Call Functions in Go.