What is '...' in Go
In Golang, an ellipsis (...
) is a syntax element that can be used in several contexts:
Variadic Functions: When used as a parameter in a function definition, it allows the function to accept a variable number of arguments of the same type. For example, the following function definition accepts an arbitrary number of integers as arguments:
Variadic Slices: When used in a function call, it can be used to "unpack" a slice and pass its elements as separate arguments. For example, the following function call passes each element of the
nums
slice as a separate argument to thesum
function:
Variadic Types: The ellipsis can also be used in the definition of a struct or array to indicate that the size of the struct or array is variable. For example, the following array definition creates an array of integers with a length that is determined at runtime:
In all cases, the ellipsis indicates that there may be an arbitrary number of values of a certain type.
Last updated