Is there a way to use a combination of splatted arguments and simple arguments (defined below) in a function taking a variadic parameter in golang? If not, is there a well-known idiom that approximates this feature? If possible, is there an idiom approximating this feature that does not require repeating the type name?
Suppose I have a function with a variadic parameter in golang of type ...T
.
It seems like your options at the call site are limited to:
- multiple simple arguments, each of type
T
, i.e.f(…… x1, x2, x3 ……)
- a single splatted argument of type
[]T
, i.ef(…… ...xs ……)
In some other languages like Python, it is possible to capture a combination of simple arguments and splatted arguments with a variadic parameter:
$ python
>>> def foo(*args): return args
...
>>> foo(1, 2, *[3, 4])
(1, 2, 3, 4)
However, the same thing does not appear to work in go.
// splat.go
package main
import "os"
import "fmt"
func write(args ...string) {
for _, item := range args {
fmt.Printf("%s
", item)
}
}
func main() {
write("foobarbaz", os.Args[1:]...)
}
from the error message, it's clear that the splatted argument and the simple argument are not being mapped to the same parameter.
$ go run splat.go
# command-line-arguments
./splat.go:14: too many arguments in call to write