Golang’s ellipsis-...-has a few different uses.
Defining variadic function parameters
What is variadic? It is a function “of unknown arity”. In plain English, that means “a function that can take an unknown number of arguments”.
A commonly-used function with variadic function parameters is Go’s builtin log.Printf. This is its signature:
func (l *Logger) Printf(format string, v ...any)The signature of Printf says it accepts a string as the first argument, and ...any means it accepts an unknown number of additional arguments of any type. This makes perfect sense if you’ve used Printf:
log.Printf("Trying endpoint: %s, attempt number %d", urlString, attemptCount)The only thing log.Printf knows for sure is that it’ll be given a string to log. It might be given any number of additional parameters to be interpolated, or it might not.
Passing slices to variadic functions
This is similar to the section above, but the syntax is slightly different. Let’s use Golang’s builtin append function, which is variadic. For reference, append behaves like this:
newSlice := append(oldSlice, anotherRecord)But what if we want to concatenate two slices? Just add ... on the right side of the second one:
func main() {
slice1 := []int{1, 2, 3}
slice2 := []int{7, 8, 9}
combined := append(slice1, slice2...)
log.Printf("combined: %v", combined)
// [1, 2, 3, 7, 8, 9]
}In this use case, Golang ellipsis behaves very similar to Javascript spread syntax, except the ... comes after the variable instead of before.
As a wildcard when running go commands
This is a non-code use of ... but it’s very common. When running go commands on the command line, ... is a wildcard meaning “everything in this directory and all subdirectories”.
go test ./services/...This command will run all test files in /services and all of its child directories, if they exist. It’s similar to file path globbing in Javascript with **.