Golang "..." Memo

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1. For arrays: Indicates the same length as the number of elements.

The length of the array in Golang is part of the type, different lengths, different types.

2. For parameters: used for formal parameter representation of variable parameters. Used for arguments to represent direct delivery. See the official documentation for specific explanation parameters:

When passing mutable parameters:

(1) If the argument is not followed by ..., the slice is created at the bottom with the same parameter type and then passed after the argument is assigned a value.

(2) If the argument is followed by ..., the slice is not created at the bottom with the same parameter type, but the argument is passed directly to the parameter.

/ref/spec#passing_arguments_to_..._parameters

Passing arguments to ... parameters

If f is variadic with a final parameter p of type ...T , then within the type of was f equivalent to p type []T. If f is invoked with no actual arguments p for, the value of passed to is p nil . Otherwise, the value passed is a new slice of type with []T a new underlying array whose successive elements are the AC tual arguments, which all must is assignable to T . The length and capacity of the slice are therefore the number of arguments bound to and could differ for each call p sit E.

Given the function and calls

Func greeting (prefix string, who ... string)
Greeting ("Nobody")
Greeting ("Hello:", "Joe", "Anna", "Eileen")

Within Greeting , would has who the value nil in the first call, and the []string{"Joe", "Anna", "Eileen"} second.

If The final argument is assignable to a slice type []T , it could be passed unchanged as the value for a ...T parameter I f The argument is followed by ... . In this case no new slice is created.

Given the slice and call s

s: = []string{"James", "Jasmine"}greeting ("Goodbye:", S ...)

Within Greeting , would has the same value as with the who s same underlying array.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.