Variable parameter functions in Go

Source: Internet
Author: User
When the last parameter of the function is * ... T* (the three points in front of *t* are intentional), called the variable parameter function *: ' gopackage mainimport ' FMT ' func sum (Numbers ... float64) (res float64) {For _, Number: = Range numbers {res + = number} Return}func main () {FMT. PRINTLN (SUM (1.1, 2.2, 3.3)} "variable parameter function allows the passing of any number of (variable) arguments, within the function through a [slice] (https://golang.org/ref/spec#Slice_types) To access the values of these parameters (for example, parameter members in the code above). > only before the last formal parameter plus ... (three points) to indicate that this is a variable parameter function. # # Arguments vs. parameters in most cases the two terms can be common to each other, but the arguments generally refer to the arguments passed in when the function/method is called, which is a parameter, especially when the function is defined. ' Gopackage mainimport ' FMT ' func sum (A, b float64) (res float64) {return a + B}func main () {FMT. PRINTLN (SUM (1.1, 1.2)} ""-A and B are formal parameters-res is the return parameter of the specified name-1.1 and 1.2 is the argument # # ... Convert to slice inside the variable parameter function, ... The type T is actually a []t: ' Gopackage mainimport ' FMT ' func f (Names ... string) {fmt. Printf ("value:% #v \ n", names) fmt. Printf ("Length:%d\n", Len (names)) Fmt. Printf ("Capacity:%d\n", cap (names)) Fmt. Printf ("Type:%t\n", names)}func main () {f ("one", "both", "three")} ' ' Compile Run code output: ' ' > Go install github.com/mlowicki/ Lab &&./bin/labvalue: []string{"One", "one", "one"), "three"}type: the consistency variable parameter function of []stringlength:3capacity:3 ' # # type is not equivalent to the last parameter being passed into the slice function: ' ' Gof: = func (... int) {}f = func ([] int) {} ' the code above will error at compile time: ' ' Src/github.com/mlowicki/lab/lab.go:17:cannot use func literal (type func ([]int)) as type Func (... int) in assignment ' # # put three points on the other side the code snippet below cannot be successfully compiled: ' ' gopackage mainimport ' FMT ' func f (Numbers ... int) {FMT. PRINTLN (Numbers)}func main () {numbers: = []int{1, 2, 3} f (Numbers)} "" ' > Go install github.com/mlowicki/lab &&am p;./bin/lab# Github.com/mlowicki/labsrc/github.com/mlowicki/lab/lab.go:11:cannot use Numbers (Type []int) as type int i n argument to F ' "This is because it is necessary to pass a single argument to an int type, so it is clear that passing a slice type is not allowed. There is a trick in Golang to solve this problem quickly: "Gopackage mainimport" FMT "func f (Numbers ... int) {FMT. PRINTLN (Numbers)}func main () {numbers: = []int{1, 2, 3} f (Numbers ...)} "' When calling function *f*, three dots (... is placed behind the argument's * *, which is an easy-to-ignore change, but it passes *numbers* as a formal parameter list. This approach implements the use of slice as a parameter to invoke the variable parameter function. A variadic function is actually a syntactic sugar, which simply takes slice as the last parameter. The variable parameter function provides a richer representation of the APIs, while also allowing developers to no longer create temporary slicE.

via:https://medium.com/golangspec/variadic-functions-in-go-13c33182b851

Author: Michałłowicki Translator: NICEDEVCN proofreading: polaris1119

This article by GCTT original compilation, go language Chinese network honor launches

This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove

271 Reads

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.