3 dots in 5 places #golang

A quick addendum to 3 dots in 4 places, which asks:

Can you name four places where three dots (…) are used in Go?

A quick TL;DR of the above is:

  • Variadic function paramaters
    That is, a function that takes a variable number of parameters – e.g. func RandomStrings( arguments ...string)
  • Arguments to variadic functions
    This is similar to javascript and the use of the “spread” operator, and allow you to pass an array as the argument to a function – e.g.

    myStrings := []string{"me", "you"}
    RandomStrings(myStrings...)

  • Array literals
    Saves some cognitive load by allowing you to use ellipses rather than counting the number of elements in an array literal definition – e.g. [...]int{1, 2, 3}
  • Go command
    Used as a wildcard

There is another…

You can also use ellipses in Go to “spread” a string into a byte array as shown below:

data := SomeStruct{}
jsonData, _ := json.Marshal(data) 
// at this point "jsonData" is of type []byte

jsonData = append(jsonData, "\r\n"...) 
// note the use of ellipses to "spread" a string into a byte array
// saves you some boilerplate casting