用于将多个“参数”映射到单个可变参数的习惯用法

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.e f(…… ...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

Create a slice with all of the arguments and splat that slice.

write(append([]string{"foobarbaz"}, os.Args[1:]...)...)

If the string, []string pattern is common in the application code, then consider writing a function to simplify the code at the call sites:

func stringArgs(s string, args []string) []string { 
   return append([]string{s}, args...)
}

...

write(stringArgs("foobarbaz", os.Args[1:])...)

or

func writeStringArgs(s string, args []string) {
    write(append([]string{"foobarbaz"}, os.Args[1:]...)...)
}

...

writeStringArgs(s, os.Args[1:])