• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Golang Ellipsis

Posted May 2, 2023 โ€” Updated Jan 10, 2024

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 **.

Filed Under: Golang

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 ยท Contact me at justin [at] {this domain}

  • Privacy Policy