3

我试图在 Go 中将可变参数从一个函数传递到另一个函数。基本上是这样的:

func CustomPrint(a ...interface{}) (int, error) {
    // ...
    // Do something else
    // ...

    return fmt.Print(a)
}

但是,当我这样做时,它a会像切片一样打印,而不是像参数列表一样。IE

fmt.Print("a", "b", "c") // Prints "a b c"
CustomPrint("a", "b", "c") // Print "[a b c]"

知道如何实现吗?

4

1 回答 1

5

使用...切片调用时使用:

package main
import "fmt"
func CustomPrint(a ...interface{}) (int, error) {
     return fmt.Print(a...)
}
func main() {
     CustomPrint("Hello", 1, 3.14, true)
}
于 2013-07-30T06:45:20.207 回答