Skip to content

Latest commit

 

History

History
42 lines (26 loc) · 1.01 KB

README.md

File metadata and controls

42 lines (26 loc) · 1.01 KB

Variadic function in Go (Practical use cases)

Variadic functions in Go (practical use cases)

Motivation

  • Accept an arbitrary number of arguments
  • Simulate optional arguments

Examples

Here are some examples of variadic function in the Go standard library

Rules

  • variadic argument has to be the last one

Pros

  • Clean and elegant API

Cons

  • Generic and ambiguous arguments and argument names
  • No automatic conversion for []interface{} aka []any

Caveats

Returning the passed slice You can’t use a variadic param as a result type, but, you can return it as a slice.

func f(nums ...int) []int { nums[1] = 10 return nums }

Resources