Go Language Example-function returns multiple values

Source: Internet
Author: User

Functions in the Go language can return multiple values, which differs greatly from other programming languages. For those with other language programming experience, the biggest obstacle is not learning this feature, but it is hard to think about using it.

Simple as an example of exchanging two values:

" FMT "  int int) (intint) {return  B, A} func main () {a: /c11>1B:2= Swap (A, b) fmt. Println (a) fmt. Println (b)}

The output is:

2

1

If in other languages, our first thought must be to create an intermediate variable for Exchange. But it is easy to use the function of returning multiple values in go.

In addition, if the declared variable is not used in go, the compilation cannot pass. So if the value returned by the multiple return value function is not required, you can use an underscore to receive the value, indicating that the value is discarded, as in the above example, only the first value that needs to be returned can be:

A, _ = Swap (A, B)

Go Language Example-function returns multiple values

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.