Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shockalotti/ef1dccc624ba39bdccc5 to your computer and use it in GitHub Desktop.
Save shockalotti/ef1dccc624ba39bdccc5 to your computer and use it in GitHub Desktop.
Go Golang - closure example, nextEven function
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())
}
@shockalotti
Copy link
Author

// 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())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment