Golang: Ellipsis(…) operator in functions

Claire Lee
2 min readAug 20, 2022

--

…type is used as the final parameter in a variadic function and slice… can be passed as a parameter to the variadic function. We can also use slice… to append function that can append the slice to another.

Golang: ellipsis operator in functions summary card

Ellipsis in variadic functions

“…type” as the final parameter in a variadic function

  • prefix ellipsis followed by a type
  • …type behaves like a slice, []type
  • If we don’t pass this parameter to the variadic function, the function will take it as an empty slice.
  • … type can only be use as the final parameter in a variadic function, otherwise it will show error, “can only use … with final parameter in list”.

Pass “slice…” as a parameter to the variadic function

  • a slice variable or a slice followed by suffix ellipsis
  • slice… behaves like slice[0], slice[1] to slice[n]

Ellipsis in append functions

append “slice…” to another

You can access to the source code here.

--

--

Claire Lee
Claire Lee

No responses yet