Reputation: 60564
Given a function declared as
func foo(bars ...string) {
// ...
}
I'd like to call this like so:
bar1 := "whiskey bar"
rest := []string{"vodka bar", "wine bar"}
foo(bar1, rest...)
but this doesn't compile; the last line errors with this message:
have (string, []string...)
want (...[]string)
Is there a way I can declare a variadic function so that it can be called with both zero or more parameters that are values, and zero or one array of values (at the end)?
Upvotes: 1
Views: 394
Reputation: 13941
You'd have to change the signature to func foo(some string, bars ...string)
as explained in the docs. More in the playground: https://play.golang.org/p/xlsCKzhj5y
Upvotes: 1