Ad space (banner)
๐ŸนGo Lessons
Lesson 26 / 68

Variadic Parameters (...)

In this lesson you will learn Go's variadic parameters so you can write a function that accepts any number of arguments. This is for anyone searching for "Go variadic parameters how to use".

Go has no default argument values as some languages do, but a variadic parameter, written as ...int, lets you write a function that takes any number of arguments. Picture a bag that holds however much you put in.

The sample code defines sum(nums ...int) int, which handles both sum(1, 2, 3) with several values and sum() with none at all. The familiar fmt.Println uses this same mechanism, which is why it works no matter how many arguments you give it.

A common stumbling block for beginners is that inside the function a variadic parameter is simply a slice. Because it is a slice, you can use slice operations on it directly.

In real-world development variadic parameters are the usual choice for a function meant to process an arbitrary number of values together.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Go directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)