What is the difference between []string and …string in golang?

huangapple go评论144阅读模式
英文:

What is the difference between []string and ...string in golang?

问题

在Go语言中,

[]string 是一个字符串数组

我们还可以使用 ...string 作为参数。

有什么区别?

函数定义:

func f(args ...string) {}

我可以像下面这样调用这个函数吗?

args := []string{"a", "b"}

f(args)
英文:

In the Go language,

[]string is a string array

and we also use ...string as a parameter.

What is the difference?

Function definition:

func f(args ...string) {}

Can I call this function like below?

args := []string{"a", "b"}

f(args)

答案1

得分: 140

[]string是一个字符串数组

从技术上讲,它是一个引用底层数组的切片

我们还使用...string作为参数。

有什么区别吗?

在结构上,实际上没有什么区别。两种语法得到的数据类型是相同的。

...参数语法创建了一个可变参数。它将接受零个或多个string参数,并将它们作为切片引用。

在调用f时,您可以使用以下语法将字符串切片传递给可变参数:

func f(args ...string) {
    fmt.Println(len(args))
}


args := []string{"a", "b"}

f(args...)

这种语法适用于使用文字语法构建的切片,或者表示可变参数的切片(因为它们之间实际上没有区别)。

http://play.golang.org/p/QWmzgIWpF8

英文:

>[]string is a string array

Technically it's a slice that references an underlying array

>and we also use ...string as a parameter.
>
>What is the difference?

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.

With respect to calling f, you can pass a slice of strings into the variadic parameter with the following syntax:

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).

http://play.golang.org/p/QWmzgIWpF8

答案2

得分: 15

两者都创建了一个字符串数组,但区别在于调用方式。

func f(args ...string) {

}
// 调用方式如下:

f("foo","bar","baz");

这允许您接受可变数量的参数(都是相同类型的)。

一个很好的例子是fmt.Print和相关函数,它可以接受任意多个参数。

英文:

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.

答案3

得分: 6

这是你想要的:

var args []string = []string{"A", "B", "C"}

func Sample(args ...string) {
    for _, arg := range args {
        fmt.Println(arg)
    }
}

func main() {
    Sample(args...)
}

Play: http://play.golang.org/p/N1ciDUKfG1

英文:

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...)
}

Play: http://play.golang.org/p/N1ciDUKfG1

答案4

得分: 2

它简化了函数参数。这是一个例子(https://play.golang.org/p/euMuy6IvaM):
方法SampleEllipsis接受零到多个相同类型的参数,但在方法SampleArray中,必须声明args

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() {
	// 方法一
	SampleEllipsis([]string{"A", "B", "C"}...)
	// 方法二
	SampleEllipsis("A", "B", "C")
    // 方法三
 	SampleEllipsis()

	// 简单数组
	SampleArray([]string{"A", "B", "C"})

	// 简单数组
	SampleArray([]string{})

}

返回:

Sample ellipsis : [A B C]
Sample ellipsis : [A B C]
Sample ellipsis : []
Sample array 
Sample ellipsis : [A B C]
Sample array 
Sample ellipsis : []
英文:

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 args to be declared.

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 : []

huangapple
  • 本文由 发表于 2012年10月16日 12:44:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/12907653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定