Skip to content

Instantly share code, notes, and snippets.

@HouLinwei
Created May 24, 2018 04:00
Show Gist options
  • Select an option

  • Save HouLinwei/6209d968b08735ba4242322cd1417760 to your computer and use it in GitHub Desktop.

Select an option

Save HouLinwei/6209d968b08735ba4242322cd1417760 to your computer and use it in GitHub Desktop.
go iterator demo
package main
import "fmt"
type Ret struct {
No int
}
func Iter(max int) func() (*Ret, bool) {
var cur int
return func() (*Ret, bool) {
for cur < max {
cur++
return &Ret{cur}, true
}
return nil, false
}
}
func RunIter() {
it := Iter(10)
for ret, ok := it(); ok; ret, ok = it() {
fmt.Println(ret.No)
}
}
func main() {
RunIter()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment