is coming soon...
ado.xyz

Pass Any Number of Parameters with Variadic Functions in Go

April 28, 2020

A variadic function allows you to accept any arbitrary number of arguments in a function. Go makes creating and working with variadic functions a breeze.

Variadic Functions in Go

The syntax for a variadic function in Go looks like this:

func cart(items ...string) {
  // implementation details
}

In the above code, let’s assume we’re writing a function to keep a list of items in our cart. We’ll keep it super simple by just keeping a list of item names.

To define a variadic function, we use the ...{type} notation, where {type} is the type of data we want to accept. So with our implementation above, we could call the function like this:


cart("pizza", "MacBook Pro", "coffee")

Go would then take our three arguments and convert them into a slice (or for those of you coming from other languages, an array). Our single items parameter would become a slice that contains the three values we passed. Let’s add some implementation details to our cart function and see what’s possible.

func cart(items ...string){
  for k, v := range items {
    fmt.Println(k)
    fmt.Println(v)
  }
}

The above code will print the value of each item as well as it’s place in the items slice.

We can add as many or as few arguments to our cart functions. But what if we had a slice of strings that we wanted to pass a slice to this function?

Passing a Slice to a Variadic Function?

If we try to do this:

items := []string{"pizza", "Macbook Pro", "coffee"}

cart(items)

We will get an error. The variadic cart function is expecting our arguments to be passed in as string values, while we’re trying to give it a slice of strings. To fix that, we simply append the variadic operator ... to the items variable. Let’s look at an example:

items := []string{"pizza", "Macbook Pro", "coffee"}

cart(items...)

The one thing to note when using this approach is that the items slice you passed into the cart function lives in the same memory space as the items slice outside of the function, so if you make any changes to the items slice in the function, they will be reflected in the original slice as well.

Mixing and Matching Static Parameters with Variadic Parameters

What if your functions calls for some static parameters as well as variadic ones? Easy. As long as your variadic parameter is the last one in the function signature, you are good to go.

To see an example of this, let’s change our cart function to take a single parameter for a user’s ID and then the items for their cart.

func cart(id int, items ...string) {
  // implementation details
}

Now when we call the cart() function, the first parameter must be of type int, and after that we can have any n number of string parameters for our users shopping cart.

cart(125, "pizza", "MacBook Pro", "coffee")
cart(150, "basketball", "hat")
cart(199)

All of the above examples will work.

To summarize, when you want a Go function to accept an arbitrary number of parameters, you would append the variadic operator ... to it. You can have a combination of variadic operators with static ones, but the variadic one must come last. Thus, you can have a max of one variadic parameter in a function.

Newsletter

Subscribe to the newsletter and be the first to know when I publish new content. No Spam.