Skip to content

Instantly share code, notes, and snippets.

@PyYoshi
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save PyYoshi/9a91752dc04798853cec to your computer and use it in GitHub Desktop.

Select an option

Save PyYoshi/9a91752dc04798853cec to your computer and use it in GitHub Desktop.
PythonのyieldをGolangで
// 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