Select single argument from all arguments of variadic function

This program demonstrates the use of a variadic function in Golang. A variadic function is a function that takes a variable number of arguments of a specific type.

In this program, the function variadicExample takes a variadic parameter of type string, indicated by the ... before the type name. This means that the function can accept any number of string arguments.

In the main function, we call variadicExample with four string arguments: "red", "blue", "green", and "yellow". These arguments are passed to the s parameter of the variadicExample function as a slice of strings.


package main

import "fmt"

func main() {
	variadicExample("red", "blue", "green", "yellow")

func variadicExample(s ...string) {



Because we are accessing the first and fourth elements of the s slice. Note that if we were to pass fewer than four arguments to variadicExample, the program would still run without error, but trying to access an index beyond the length of the slice would result in a runtime error.

Most Helpful This Week