Essential Go Functions  Suggest an edit

Variadic functions

A variadic function can be called with any number of trailing arguments. Those elements are stored in a slice.

func variadic(strs ...string) {
	// strs is a slice of string
	for i, str := range strs {
		fmt.Printf("%d: %s\n", i, str)
	}
	fmt.Print("\n")
}

func main() {
	variadic("Hello", "Goodbye")
	variadic("Str1", "Str2", "Str3")

	// you can also give a slice to a variadic function, with `...`:
	strs := []string{"a", "b"}
	variadic(strs...)
}
0: Hello
1: Goodbye

0: Str1
1: Str2
2: Str3

0: a
1: b

  ↑ ↓ to navigate     ↵ to select     Esc to close