This is a creation in Article, where the information may have evolved or changed.
have been using Java has not been closed (I am also very vegetable, maybe Java can also be closed I will not just, I hope someone replies pointing), the new contact Golang on its closure mechanism is also very confused. The following six versions of the closure of a little talk about. There are several versions from other sources, I have seen for some time I do not remember the source is very sorry to the author!
Version 1:
Package Mainimport "FMT" Func Main () {var fn [10]func () for I: = 0; i < len (FN); i++ {Fn[i] = func () {fmt. Println (i)}}for _, F: = range fn {f ()}}
The results are as follows:
10101010101010101010
Analysis: Mian () and Func () [] arrays constitute closures using the same I variable main function does not exit I variable always exists, F () executes when the print statement is called at this time the variable i is 10.
Version 2:
Package Mainimport "FMT" Func Main () {var fn [10]func () for I: = 0; i < len (FN); i++ {Fn[i] = func () {fmt. Println (i)}}for I: = 0; I < Len (FN); i++ {Fn[i] ()}}
The results are as follows:
10101010101010101010
Analysis: In contrast to version 1, the I variable shown is used as an iteration, but I in the closure space is different from I in the call iteration (the living space is also different) so I in the closure space is used as the print value of 10.
Version 3:
Package Mainimport "FMT" Func Main () {var fn [10]func () for I: = 0; i < len (FN); i++ {Fn[i] = func () {fmt. Println (i)}}for j: = 0; J < Len (FN); J + + {Fn[j] ()}}
The results are as follows:
10101010101010101010
Analysis: To prove the analysis in version 2, using J as the iteration variable, the same prints 10.
version 4:
Package Mainimport "FMT" Func Main () {var fn [10]func () var i intfor i = 0; i < len (FN); i++ {Fn[i] = func () {fmt. Println (i)}}for i = 0; I < Len (FN); i++ {Fn[i] ()}}
The results are as follows:
0123456789
Analysis: Declaring the variable i in main () at this time I's living space expands to the main () function, two iterations using the same I variable. Therefore, the iteration current value of I in the second iteration is used as the printing parameter.
Version 5:
Package Mainimport "FMT" Func Main () {var fn [10]func () for I: = 0; i < len (FN); i++ {Fn[i] = MAKE_FN (i)}for _, F: = Ran GE fn {f ()}}func make_fn (i int) func () {return func () {fmt. Println (i)}}
The results are as follows:
0123456789
Analysis: Define a separate closure function outside of the main () function to form a separate closure unit that isolates different func () in different func () []. Isolation and independence are the meaning of closures, and a collection that represents a series of States should not change the internal state of an external display notification when it changes.
Version 6:
Package Mainimport "FMT" Func Main () {var fn [10]func (int.) for I: = 0; i < len (FN); i++ {Fn[i] = MAKE_FN ()}for I, F: = R ANGE fn {f (i)}}func make_fn () func (i int) {return func (i int) {FMT. Println (i)}}
The results are as follows:
0123456789
Analysis: The last version should be the best, and the closure example in Gotour is exactly how this is represented.
Limited ability can only write so much, I first send Bo support, have comments say thank you (abusive also)!