Created
May 24, 2018 04:00
-
-
Save HouLinwei/6209d968b08735ba4242322cd1417760 to your computer and use it in GitHub Desktop.
go iterator demo
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" | |
| 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