Go function Summary (GO)

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

The Go language function is a little bit different from C,java and so on, roughly as follows:

Definition of a function

For example:

Func Max (A, b int) int{//returns the largest one within a, and note how the function's arguments and return value types are defined    if b>a{        return b    }    return a}

Second, multi-value return

A function in the go language can have multiple return values.

One of the simplest examples

Package Mainimport (    "FMT") var a intfunc main () {    b: = []byte{1, 2, 3, 4}    var x int. for    I: = 0; I << /SPAN> Len (b); {        x, i = Nextint (b, i)//The return result of the final i is Len (b)        FMT.  PRINTLN (x)    }}func nextint (b []byte, I int) (int, int) {    x: = 0 for    ; I <</SPAN> Len (b); i++ {        x = x*10 + int (b[i])    }    return x, i} The result is: 1234
Three, variable function parameters
Examples are as follows:
Package Mainimport (    "FMT") func main () {    sum (1, 2)    sum (1, 2, 3)    //pass array    nums: = []int{1, 2, 3, 4}
  sum (Nums ...)} Func sum (nums ... int) {    fmt. Print (Nums, "")//output such as [1, 2, 3] array    total: = 0 for    _, num: = range Nums {//want to be a value instead of subscript total        + = num    }    FMT. Println (Total)}
Four, function closure
Nextnum This function returns an anonymous function that remembers the value of I+j in Nextnum and changes the value of i,j, thus forming a closure usage with the following code:
Package Mainimport (    "FMT") func Nextnum () func () int {    I, J: = 1, 1    return func () int {        var tmp = i + j
  i, J = j, tmp        return tmp    }}//main function is a call to Nextnum, which is mainly to play the next Fibonacci number Func main () {    Nextnumfunc: = Nextnum () C11/>for I: = 0; I <</SPAN> 10; i++ {        FMT. Println (Nextnumfunc ())    }}
V. Named return value
Fan () is a function of factorial, and its return value parameters and types are declared when the function is defined. The code is as follows:
Package Mainimport (    "FMT") func main () {    fmt. Println (Fan (5))//factorial}func fan (x int) for output 5 (result int) {//return value = result int type    if x = = 0 | | x = = 1 {        result = 1    } el se {        result = x * Fan (x-1)    }    return//Returns the result in this case}

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.