Golang Indeterminate parameters

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

1, fixed type of indefinite parameters

Syntax format: ArgumentName ... Type

Ackage Main
Import "FMT"
Func F1 (args ... int) {
For _, V: = Range args {
Fmt. Println (v)
}
}
Func Main () {
F1 ()
F1 (1)
F1 (3, 5)
}

2, any type of indefinite parameters

interface{}: null interface type because any type implements an empty interface, so any type of object can be assigned to an empty interface.

Package Main
Import "FMT"
Func F1 (args ... interface{}) {
For _, V: = Range args {
Fmt. Println (v. (int))//The interface type assertion is used here
}
}
Func Main () {
F1 ()
F1 (1)
F1 (3, 5)
}

3, the transfer of indefinite parameters

Assuming that args is an indeterminate parameter, you can pass the argument to another function with an indeterminate argument

Func F1 (args ... int) {
F2 (args ...)
F2 (args[1:] ...)
}
Func F2 (args ... int) {
//

}

173 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.