98

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)
030
  • 10,842
  • 12
  • 78
  • 123
user1746360
  • 1,164
  • 2
  • 8
  • 12

4 Answers4

140

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

Community
  • 1
  • 1
I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77
  • 12
    `[]string` is a slice, not an array. The differences between an array and slice are subtle but important. – Stephen Weinberg Oct 16 '12 at 06:02
  • @StephenWeinberg: Yes, my "nothing really" answer to the "what's the difference" quote is answering the question that was asked about the difference between the slice generated by the variadic function parameter, and the one created using the `[]string` syntax. I'll add more of the quote to my answer to make it clearer. :-) – I Hate Lazy Oct 16 '12 at 11:16
  • @IHateLazy Is there a way to make builtin `println` work with ellipsis? [Here](http://play.golang.org/p/lJKJbhC1a1) you can find my experiments. If someone wishes some debug print functions, go watch my playgound. – vault Aug 12 '14 at 16:48
16

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.

tylerl
  • 30,197
  • 13
  • 80
  • 113
6

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

Ertuğrul
  • 69
  • 1
  • 1
2

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 : []
Petre Sosa
  • 2,097
  • 1
  • 23
  • 19