Skip to content

Instantly share code, notes, and snippets.

@legendtkl
Created September 6, 2016 06:58
Show Gist options
  • Select an option

  • Save legendtkl/1061b7e3d0becf45cb6bfabf78a292cf to your computer and use it in GitHub Desktop.

Select an option

Save legendtkl/1061b7e3d0becf45cb6bfabf78a292cf to your computer and use it in GitHub Desktop.

Revisions

  1. legendtkl created this gist Sep 6, 2016.
    16 changes: 16 additions & 0 deletions closure1.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    package main

    import (
    "fmt"
    )

    func main() {
    for i:=0; i<10; i++ {
    go fmt.Println(i)
    }

    for i:=0; i<10; i++ {
    j := i
    go fmt.Println(j)
    }
    }
    22 changes: 22 additions & 0 deletions closure2.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    package main

    import (
    "fmt"
    )

    func makeEvenGenerator() func() uint {
    i := uint(0)

    return func() (ret uint) {
    ret = i
    i += 2
    return
    }
    }

    func main() {
    nextEven := makeEvenGenerator()
    fmt.Println(nextEven())
    fmt.Println(nextEven())
    fmt.Println(nextEven())
    }