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)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Technically it’s a slice that references an underlying array
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 morestringarguments, 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: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