Indefinite parameter usage of functions in go

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

For detailed usage, please refer to "Go Language Programming" (Xu Xiwei, Beijing: People's post and Telecommunications Press, 2012.08) Section 2.5.3 "Indeterminate parameters".

Here are some of the experiences that I use:

You can interpret an indeterminate parameter as an array, you can organize an array or slice yourself, and then pass it as an indeterminate parameter to a function that accepts indeterminate arguments.

Assuming that there is another variable called myfunc3 (args ... int), the following shows how to organize an indeterminate parameter yourself:

Func myfunc (arg1 int,arg2 int, args ... int) {

Argsnew: =make ([]int,2+len (args)

Argsnew[0]=arg1

Argsnew[1]=arg2

For I:=0;i<len (args); i++ {

Argsnew[i+2]=args[i]

}

Myfunc3 (Argsnew ...) Note that you must add "..." to indicate that an indeterminate parameter is being transmitted instead of an array
}


Reference article:

See Paradigm Golang (14)-variadic function: Http://ieqi.net/2013/03/11/%E7%9C%8B%E8%8C%83%E4%BE%8B%E5%AD%A6-golang-%EF%BC%88%E5%8D %81%e5%9b%9b%ef%bc%89-%e5%8f%af%e5%8f%98%e5%8f%82%e6%95%b0%e5%87%bd%e6%95%b0/

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.