Note - This tutorial is a follow on from my previous article on regular functions in go: Go Functions Tutorial
In this tutorial, we are going to be looking at
Variadic Functions in Go. We’ll be covering how to implement and how to use these functions within your own Go applications.
There are times, when we do not know how many string arguments we will require for our functions. This is where
variadic functions come into play.
Variadic functions allow us to define functions that take in an arbitrary number of arguments. This prevents us having to code against every possible variation of input length and this concept transfers across a wide number of different languages such as Python and in Java.
If we attempt to run this, we should see that our call to
fmt.Println() will print an array of strings that include both
Note - Variadic Functions are not limited to strings, we can use any variation of composite or basic types.
Let’s have a look at an example of this in production Go code.
Println() is a great example of a variadic function which is possibly the most well known.
Note - This example takes in an arbitrary number of
So, in this tutorial, we managed to successfully cover variadic functions in Go. We covered, what they are and how they can be used for fame and fortune within your own Go programs.
If you enjoyed this tutorial, or have any suggestions, I would love to hear them in the suggestions/comments section below!
Note - If you want to keep up to date with the latest articles and updates on the site then please feel free to follow me on twitter: @Elliot_f