golang 中的 []string 和 ...string 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12907653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between []string and ...string in golang?
提问by user1746360
In the Go language,
在 Go 语言中,
[]string
is a string array
[]string
是一个字符串数组
and we also use ...string
as a parameter.
我们也...string
用作参数。
What is the difference?
有什么不同?
Function definition:
函数定义:
func f(args ...string) {}
Can I call this function like below?
我可以像下面这样调用这个函数吗?
args := []string{"a", "b"}
f(args)
回答by I Hate Lazy
[]string
is a string array
[]string
是一个字符串数组
Technically it's a slice that references an underlying array
从技术上讲,它是一个引用底层数组的切片
and we also use
...string
as a parameter.What is the difference?
我们也
...string
用作参数。有什么不同?
With respect to the structure, nothing really. The data type resulting from both syntax is the same.
至于结构,真的没什么。两种语法产生的数据类型是相同的。
The ...
parameter syntax makes a variadic parameter. It will accept zero or more string
arguments, and reference them as a slice.
该...
参数语法形成了可变参数的参数。它将接受零个或多个string
参数,并将它们作为切片引用。
With respect to calling f
, you can pass a slice of strings into the variadic parameter with the following syntax:
关于调用f
,您可以使用以下语法将一段字符串传递到可变参数参数中:
func f(args ...string) {
fmt.Println(len(args))
}
args := []string{"a", "b"}
f(args...)
This syntax is available for either the slice built using the literal syntax, or the slice representing the variadic parameter (since there's really no difference between them).
此语法可用于使用文字语法构建的切片或表示可变参数的切片(因为它们之间实际上没有区别)。
回答by tylerl
Both create an array of strings, but the difference is in how it is called.
两者都创建了一个字符串数组,但不同之处在于它的调用方式。
func f(args ...string) {
}
// Would be called like this:
f("foo","bar","baz");
This allows you to accept a variable number of arguments (all of the same type)
这允许您接受可变数量的参数(所有相同类型)
A great example of this is fmt.Print
and friends, which can accept as few or as many arugments as you want.
一个很好的例子是fmt.Print
和朋友,它们可以接受任意数量或任意数量的参数。
回答by Ertu?rul
Here is what you want:
这是你想要的:
var args []string = []string{"A", "B", "C"}
func Sample(args ...string) {
for _, arg := range args {
fmt.Println(arg)
}
}
func main() {
Sample(args...)
}
回答by Petre Sosa
It simplifies your function parameters. Here is an example(https://play.golang.org/p/euMuy6IvaM): Method SampleEllipsis accepts from zero to many parameters of the same type but in the method SampleArray it is mandatory argsto be declared.
它简化了您的函数参数。这是一个示例(https://play.golang.org/p/euMuy6IvaM):方法 SampleEllipsis 接受从零到多个相同类型的参数,但在方法 SampleArray 中,它是必须声明的参数。
package main
import "fmt"
func SampleEllipsis(args ...string) {
fmt.Printf("Sample ellipsis : %+v\n",args)
}
func SampleArray(args []string) {
fmt.Println("Sample array ")
SampleEllipsis(args...)
}
func main() {
// Method one
SampleEllipsis([]string{"A", "B", "C"}...)
// Method two
SampleEllipsis("A", "B", "C")
// Method three
SampleEllipsis()
// Simple array
SampleArray([]string{"A", "B", "C"})
// Simple array
SampleArray([]string{})
}
Returns :
回报:
Sample ellipsis : [A B C]
Sample ellipsis : [A B C]
Sample ellipsis : []
Sample array
Sample ellipsis : [A B C]
Sample array
Sample ellipsis : []