If I have a function where the last argument is optional, is it an appropriate practice to use ...
to allow the argument to be optional, or is it considered bad form?
Example:
func Foo(s ...string) {
switch len(s) {
case 0:
fmt.Println("You didn't pass an argument")
case 1:
fallthrough
default:
fmt.Printf("You passed %s
", s[0])
}
}
Foo("bar") // "You passed bar"
Foo() // "You didn't pass an argument"
Foo("bar", "baz") // "You passed bar"
In this example, I don't care if too many arguments were passed, but I could handle that in the default:
case when needed.