Created
September 6, 2016 06:58
-
-
Save legendtkl/1061b7e3d0becf45cb6bfabf78a292cf to your computer and use it in GitHub Desktop.
golang closure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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