Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 如何将变长参数作为参数传递给Golang中的另一个函数?_Function_Go_Variadic Functions - Fatal编程技术网

Function 如何将变长参数作为参数传递给Golang中的另一个函数?

Function 如何将变长参数作为参数传递给Golang中的另一个函数?,function,go,variadic-functions,Function,Go,Variadic Functions,如何在Go中传递可变长度参数?例如,我想打电话 func MyPrint(format string, args ...interface{}) { fmt.Printf("[MY PREFIX] " + format, ???) } // to be called as: MyPrint("yay %d", 213) // or MyPrint("yay") // or MyPrint("yay %d %d",123,234)

如何在
Go
中传递可变长度参数?例如,我想打电话

func MyPrint(format string, args ...interface{}) {
  fmt.Printf("[MY PREFIX] " + format, ???)
}

// to be called as: MyPrint("yay %d", 213) 
//              or  MyPrint("yay")
//              or  MyPrint("yay %d %d",123,234)

啊,找到了…接受可变长度参数的函数称为变量函数。例如:

package main

import "fmt"

func MyPrint(format string, args ...interface{}) {
  fmt.Printf("[MY PREFIX] " + format, args...)
}

func main() {
 MyPrint("yay %d %d\n",123,234);
 MyPrint("yay %d\n ",123);
 MyPrint("yay %d\n");
}

对于那些想要引用的人…请注意,对MyPrint的最终调用缺少一个参数,因此fmt将抱怨%d要打印的参数缺少