Tomas Aschan
Tomas Aschan

Reputation: 60564

Is it possible to "partially apply" variadic functions in go?

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

Answers (1)

Michael Hausenblas
Michael Hausenblas

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

Related Questions