Looking for go Answers? Try Ask4KnowledgeBase
Looking for go Keywords? Try Ask4Keywords

Go Variadic functions


Example

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

package main

import "fmt"

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

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

play it on playground

You can also give a slice to a variadic function, with ...:

func main() {
     strs := []string {"Str1", "Str2", "Str3"}

     variadic(strs...)
}

play it on playground