Golang Closure Package

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

1 Form Meanings

Listening to the so-called closures is a function of "capturing" and other constants and variables that it uses in the same way.

Listen formally, in Golang, all anonymous functions are closures. Closures are created in almost the same way as normal functions, with only one key difference: closures do not have a name.

Listen, let's see two examples.

Addpng Listen: = Listen to Func (name listen string) listen to string Listen {Listen to return listen to name Listen + listen to ". png" Listen to}addjpg listen: = Listen to Func (name listen string) listen to string Listen {Listen to the name listen ". jpg" Listen to}filename listen: = Listen to "abc" FMT. PRINTLN (addpng (filename), listening to addjpg (filename))

Listen to the results as follows

Listen to the factory function, the return value is also a function funcaddsuffix (suffix listen to string) listen to Func (string) listen to the string listen {Listen to listen to hear the Return of Func (name listen string) listen to the string listen { Listen and listen to hear if listen to!strings. Hassuffix (name, listen to suffix) Listen {Listen, listen, listen, hear, listen, listen, hear-listen to the name, hear, listen, listen, hear, listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listening to listen to hear to listen to listen to listen to listen to listen to listening to listen Listen to the Func listen to main () listen {Listen to listen to listen to hear ... Listen and listen to addzip listen: = Listen to Addsuffix (". zip") listen and listen to the addtgz listen: Listen to the Addsuffix (". tar.gz") listen and listen to the FMT. PRINTLN (addzip (filename), addtgz (filename))}

2 substantive meaning

It is not enough to simply understand the closure as an anonymous function in the form of formality, but also to understand the meaning of closures in essence.

Listen essentially, closures are entities that are composed of functions and their associated reference environments (that is, closures = functions + reference environments). Closures can have multiple instances at run time, and different reference environments and the same combination of functions can produce different instances. By the nature of the closure, we can infer that the closure acquisition capture variable is equivalent to a reference pass, not a value pass; for the constants and variables captured by the closure function, the closures can use these constants and variables, regardless of when and where the closures are called, without caring about the scopes on their surfaces.

Listen, we use an example to verify.

Funcaddnumber (x Listen int) listen to func (int) Listen {Listen and listen to the FMT. Printf ("x: Listen to%d, listen to addr listen to the x:%p\n", listen to X, listen to &x) listen to listen to the return to the func (y listen int) Listen to listen to listen to hear "listen to" hear "listen" listening to listening to listen to listening to hear the x listen, listen and listen to listen to listen to listen to the X Listen to K listen and listen and listen to the FMT. Printf ("x: Listen to%d, listen to addr listen to x:%p\n", listen to X, listen to &x) listen and listen and listen to the FMT. Printf ("Y: Listen%d, listen to addr listen to y:%p\n", listen to Y, listen to &y) listen and listen to Func listen to the main () listen {Listen to listen to the addnum listen to: = Listen to the Addnumber (5) Listen to listen to the Addnum (1) Listen and listen to Addnum (1) Listen and hear Addnum (1) Listen and listen to the FMT. Println ("---------------------") listen and listen to ADDNUM1 listen to listen to Addnumber (5) Listen and listen to ADDNUM1 (1) Listen and listen to ADDNUM1 (1) Listen and hear AddNum1 (1)}

Listen to the running results

listen to the first point of emphasis, x is the variable captured in the closure, y is only a local variable inside the closure, not the variable being captured. Therefore, for each reference,the address ofx is fixed, the same reference variable, and the address ofy is changed. In addition, the closure was quoted two times, resulting in two closure instances, namely addnum: = Addnumber (5) and addNum1: =addnumber (5) is two different instances in which the two x variables referenced are also from two different instances.


This article from "Talking Cabbage" blog, declined reprint!

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.