Golang支持可变参数功能吗? [英] Does Golang support variadic function?
本文介绍了Golang支持可变参数功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
类似于这样的
func Add(num1 ... int)int {
return args
}
func main(){
fmt.Println(Hello,playground)
fmt.Println(Add(1,3,4,5,))
}
我想为任意数量的输入概括添加函数。
解决方案
从我所知道的情况来看,你几乎可以得到它,但语法是 ... int
。请参阅规范:
< blockquote>
给定函数并调用
func Greeting(前缀字符串,who ...字符串)
问候(hello:,Joe,Anna,Eileen)
在问候语中, who
将具有值 [] string {Joe,Anna,Eileen}
I wonder is there anyway that I can define a function for unknown number of variables in Go.
Something like this
func Add(num1... int) int {
return args
}
func main() {
fmt.Println("Hello, playground")
fmt.Println(Add(1, 3, 4, 5,))
}
I want to generalize add function for any number of inputs.
解决方案
You've pretty much got it, from what I can tell, but the syntax is ...int
. See the spec:
Given the function and call
func Greeting(prefix string, who ...string) Greeting("hello:", "Joe", "Anna", "Eileen")
within Greeting,
who
will have the value[]string{"Joe", "Anna", "Eileen"}
这篇关于Golang支持可变参数功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文