如何在中传递可变长度参数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)
1 回答
www说
TA贡献1775条经验 获得超8个赞
啊,发现了……接受可变长度参数的函数称为Variadic Functions。例子:
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");
}
- 1 回答
- 0 关注
- 130 浏览
添加回答
举报
0/150
提交
取消