Go language Learning-anonymous functions and closures

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

The go language supports anonymous functions, where functions can be passed or used like normal variables.

Use the following method

A: = func (input1 int) int{

return INPUT1 + = 1

}()

B: =a (2)


The anonymous function of the go language is closures, and the following is the explanation of closures in go language programming

Basic concepts
Closures are blocks of code that can contain variables that are free (not bound to specific objects) that are not within this block of code or
Defined in any global context, but in the context in which the code block is defined. The code block to execute (because the free variable contains
In the code block, so these free variables and the objects they refer to are not freed) to provide a binding calculation ring for the free variable
Environment (SCOPE).
The value of closures
The value of a closure is that it can be used as a function object or as an anonymous function, which means, for a type system, not only to represent
The data also represents the code. Most languages that support closures use functions as first-level objects, meaning that these functions can be stored in
Variables are passed as parameters to other functions, most importantly, they can be dynamically created and returned by the function.




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.