Last active
August 29, 2015 14:22
-
-
Save PyYoshi/9a91752dc04798853cec to your computer and use it in GitHub Desktop.
PythonのyieldをGolangで
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
| // Thanks mattn_jp!!: https://twitter.com/mattn_jp/status/605581283238944768 http://play.golang.org/p/Tzaoh3liuN | |
| package main | |
| import "fmt" | |
| type Tower struct { | |
| Floor int | |
| } | |
| func (t Tower) String() string { | |
| return fmt.Sprintf("勇者は%d階を攻略した. 🎉🎉🎉", t.Floor) | |
| } | |
| func GoUpstairs(floors int) chan Tower { | |
| ch := make(chan Tower) | |
| go func() { | |
| floor := 1 | |
| for floor <= floors { | |
| ch <- Tower{floor} | |
| floor++ | |
| } | |
| close(ch) | |
| }() | |
| return ch | |
| } | |
| func main() { | |
| for x := range GoUpstairs(100) { | |
| fmt.Println(x) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment