github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2013/go4python/genex.go (about) 1 // +build OMIT 2 3 package main 4 5 import "fmt" 6 7 // prime returns true if n is a prime number. 8 func prime(n int) bool { 9 for i := 2; i < n; i++ { 10 if n%i == 0 { 11 return false 12 } 13 } 14 return true 15 } 16 17 // primes returns a channel of ints on which it writes the first n prime 18 // numbers before closing it. 19 func primes(n int) chan int { 20 c := make(chan int) 21 go func() { 22 for i := 1; n > 0; i++ { 23 if prime(i) { 24 c <- i 25 n-- 26 } 27 } 28 close(c) 29 }() 30 return c 31 } 32 33 func main() { 34 for p := range primes(10) { 35 fmt.Println(p) 36 } 37 }