A classic example of closure golang

Transfer description link
https://www.golangtc.com/t/50d07e11320b521f59000012

While closures used in the development not by much, but still understand the significance of programming helpful for
version 1:

package main

import "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:

10
10
10
10
10
10
10
10
10
10

Analysis: mian () and FUNC () [] array that consists of a closure using the same variable i does not exit main function variables exist i, f () calls the print statement is executed at this time the variable i is 10. Version 2:

package main

import "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:

10
10
10
10
10
10
10
10
10
10

Analysis: Comparative Version 1 shown in use as a variable iteration i, the iteration i and call closure spaces in the i point to different memory (different from the living space) so the use of closures as the print space is 10 i . Version 3:

package main

import "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:

10
10
10
10
10
10
10
10
10
10

Analysis: To demonstrate version 2 of the statement analysis, using j as the iteration variable, 10. 4 print version of the same:

package main

import "fmt"

func main() {

    var fn [10]func()
    var i int

    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:

0
1
2
3
4
5
6
7
8
9

Analysis: In the main () declare the variable i at this time to expand the living space i of the main () function, two iterations using the same i variable. Therefore, in the second iteration the current value of the iteration i will be used as the print parameters. Version 5:

package main

import "fmt"

func main() {

    var fn [10]func()

    for i := 0; i < len(fn); i++ { 
        fn[i] = make_fn(i)
    }

    for _, f := range fn {
        f()
    }
}

func make_fn(i int) func() {
    return func() {
        fmt.Println(i)
    }
}

The results are as follows:

0
1
2
3
4
5
6
7
8
9

Analysis: main () function is defined in individual outer closure function, the closure means constitutes a separate, isolate different FUNC () [] different func (). Significance is isolated and independent closures where, a denotes a series of state change should not set its internal state change notification is displayed outside. Version 6:

package main

import "fmt"

func main() {

    var fn [10]func(int)

    for i := 0; i < len(fn); i++ { 
        fn[i] = make_fn()
    }

    for i, f := range fn {
        f(i)
    }
}

func make_fn() func(i int) {
    return func(i int) {
        fmt.Println(i)
    }
}

The results are as follows:

0
1
2
3
4
5
6
7
8
9

Analysis: This final version should be the best, gotour the closure of which is the representation of an example of use.

Guess you like

Origin www.cnblogs.com/hirampeng/p/11279898.html