Created
May 28, 2014 01:22
-
-
Save shockalotti/ef1dccc624ba39bdccc5 to your computer and use it in GitHub Desktop.
Go Golang - closure example, nextEven function
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
// Odd number generator
package main
import "fmt"
func makeOddGenerator() func() uint {
i := uint(1)
return func() (ret uint) {
ret = i
i += 2
return
}
}
func main() {
nextOdd := makeOddGenerator()
fmt.Println(nextOdd())
fmt.Println(nextOdd())
fmt.Println(nextOdd())
}