Small Talk Golang closure

Source: Internet
Author: User
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)!

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.