Golang ProgramsGolang Programs

Select single argument from all arguments of variadic function.

In below example we will are going to print s[0] the first and s[3] the forth, argument value passed to variadicExample() function.
package main

import "fmt"

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

func variadicExample(s ...string) {
	fmt.Println(s[0])
	fmt.Println(s[3])
}
C:\golang\example>go run test1.go
red
yellow

C:\golang\example>
Needs to be precise when running an empty function call, if the code inside of the function expecting an argument and absence of argument will generate an error "panic: run-time error: index out of range". In above example you have to pass at least 4 arguments.