Skip to content

Instantly share code, notes, and snippets.

@legendtkl
Created September 6, 2016 06:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save legendtkl/1061b7e3d0becf45cb6bfabf78a292cf to your computer and use it in GitHub Desktop.
golang closure
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)
}
}
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())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment