This is a creation in Article, where the information may have evolved or changed.
First, anonymous function
Definition: A function without a function name.
Role: The current understanding in the go language is used to form closures.
* Note: Since JS does not have block-level scopes, anonymous functions are often used to contain code to not pollute the global namespace and destroy the environment after running.
----from the answer: http://www.zhihu.com/question/34649602
Use method and its principle please refer to: http://www.cnblogs.com/chenxianbin89/archive/2010/01/28/1658392.html
Examples of Use
(1)
A: = Func () {fmt. PRINTLN (1)}a ()//output: 1
(2) with parameters
B: = func (arg int) {FMT. Println (ARG)}b (2)//output: 2 (func (arg int) {FMT. Println (ARG)}) (3)//output: 3
(3) with return value
c: = func () int {fmt. PRINTLN (4) return 5}d: = C ()//Print out 4 and assign 5 to dfmt. Println (d)
Second, closure (closure)
Closure of the understanding reference: http://www.cnblogs.com/mzwr1982/archive/2012/05/20/2509295.html
Use of closures reference: http://blog.csdn.net/sunlylorn/article/details/6534610
and http://www.cnblogs.com/rainman/archive/2009/05/04/1448899.html
In simple terms:
Because the returned function is assigned a variable, although the function destroys its execution environment at the end of the execution,
However, if there is a closure, the closure saves the active object (variable) of the external function, so if you do not remove the reference to the closure,
Closures persist in memory, and the garbage collector does not destroy the memory that is consumed by closures.
----from the answer http://www.zhihu.com/question/34649602
Examples of Use
(1)
Function A is a non-parameter, the return value is an anonymous function, and the function//with an int type parameter, the return value is an int type func a () func (aa int) int {sum: = 0return func (cc int) int {sum + = cc Fmt. Println ("aa=", AA, "bb=", BB, "sum=", sum) return sum}}//compile error, prompt AA undefined
In fact, the func (AA int) int is just the return value of function A, where the name of the parameter does not have any effect, but it will affect the code reading, directly with the func (int) int.
After correction:
Func A () func (int) int {sum: = 0return func (bb int) int {sum + = bbfmt. Println ("bb=", BB, "\tsum=", sum) return sum}}
Call 1:
Func Main () {A: = a ()//define variable A and assign the return value of function A to AB: = A (4) fmt. Println (b)}/*** output: * * bb= 4 sum= 4** 4*/
Call 2:
Func Main () {A: = A () a (0) a (1) a (5)}/*** output: * * bb= 0 sum= 0** bb= 1 sum= 1** bb= 5 sum= 6*/
The above call realizes the summation of sum by the closure package.
Call 3:
Func Main () {A: = A () c: = A () a (0) a (5) C (Ten) C (20)}/*** Output: * * bb= 0 sum= 0** bb= 5 sum= 5** bb= ten sum= 10** bb= */
As you can see, the previous example calls two function A, which makes up two closures, and the variable sum maintained by these two closures is not the same variable.
(2)
Func B () []func () {b: = make ([]func (), 3, 3) for I: = 0; i < 3; i++ {b[i] = func () {fmt. Println (i)}}return B}func main () {c: = B () c[0] () c[1] () c[2] ()}/*** Output: * * 3** 3** 3*/
Closures use variables of external functions in the way they are referenced .
In the example above, only one function B is called, which forms a closure, I is defined in the outer function B, so that I in the closure maintenance of the variable I, c[0], c[1], c[2] is a reference to I in the closure.
Therefore, after executing c:=b (), the value of I has changed to 3, so the output when calling C[0] () is 3 instead of 0.
Can be modified as follows:
Func b () []func () {b: = make ([]func (), 3, 3) for I: = 0; i < 3; i++ {B[i] = (func (j int) func () {return func () {fmt. Println (j)}) (i)}return B}func main () {c: = B () c[0] () c[1] () c[2] ()}/*** output: * * 0** 1** 2*/
The above modifications may not be meaningful and are used only for illustrative issues.
Similar problems may occur when using defer, and it is important to note that:
For j: = 0; J < 2; J + + {Defer (func () {fmt. Println (j)}) ()}/*** output: * * 2 * * 2*/