Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created January 19, 2019 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmota/7be1f7e47bc5910678861b1eb0783a34 to your computer and use it in GitHub Desktop.
Save miguelmota/7be1f7e47bc5910678861b1eb0783a34 to your computer and use it in GitHub Desktop.
Golang javascript-like generator
package main
import "fmt"
func Generator() chan string {
c := make(chan string)
go func() {
c <- "hello"
c <- "world"
close(c)
}()
return c
}
func main() {
gen := Generator()
for true {
value, more := <-gen
fmt.Println(value, more)
if !more {
break
}
}
// alternatively
for value := range Generator() {
fmt.Println(value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment