Golang Learning Note function

Source: Internet
Author: User
Tags closure
function Declaration

The function uses the FUNC keyword declaration, except that the type is post-built, and the rest is similar to the rest of the language. In particular, like variable declarations, if the type of a function parameter is the same, you can add the type only at the end.

Func Add (A, b int) int {
    return a + b
}
function return value

The Golang function can return multiple values, which are similar to the attributes of the returned tuples in some languages.

Func Swap (A, b int) (int, int) {
    return B, a
}
Named return value

The named return value is somewhat like an out parameter for some languages. With a named return value, the return statement does not need to add the returned value, because the return value is already specified on the function signature.

Func Divide (A, b int) (quotient, remainder int) {
    quotient = A/b
    remainder = a% B
    return
}
Recursive Functions

Recursive functions are functions that call themselves, and recursive functions need to have termination conditions to avoid infinite recursion. Here is an example of recursion for factorial.

Func facterial (n int) int {
    if n = = 1 | | n = = 0 {
        return 1
    } else {
        return facterial (n-1) * N
    }
  }
function as a variable

Functions can be used as variables or as function parameters in Golang, so that functions can be used very flexibly. For example, the following function takes a function as a parameter and invokes the function with the given argument.

Func Compute (fun func (int, int) int, a int, b int) {
    fmt. PRINTLN ("Calculation result:", Fun (A, B))
}

We can use the following code to invoke the example above.

    Fmt. Println ("function as parameter")
    add: = Func (A, b int) int {
        return a + b
    }
    Compute (ADD, 3, 4)
    Subtract: = Func (A, B int) int {
        Return a-b
    }
    compute (Subtract, 3, 4)
function Closures

If an inner function refers to the value of an outer function, then the inner function becomes a closure. The external variable lifetime referenced in the closure is extended, and the variables will continue to exist even if the external function has ended.

Func Getcounter () func () int {
    I: = 0
    return func () int {
        i++
        return i
    }
}

If the closure returned by the above function is called consecutively, each counter will be incremented.

Counter: = Getcounter ()
FMT. Println (Counter ())
FMT. Println (Counter ())
FMT. Println (counter ())

The

function closures have similar concepts in many programming languages. If you already know about closures, the examples above will be easy to understand.

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.