/src/thread/gen.go

https://code.google.com/p/goexample/ · Go · 27 lines · 15 code · 8 blank · 4 comment · 3 complexity · b8e9a5d79b1ec3663aa4a84889066c34 MD5 · raw file

  1. // Copyright 2010 The Go Example Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import "fmt"
  6. func generate(ch chan int){
  7. for i:=2;;i++ {
  8. ch <- i
  9. }
  10. }
  11. func main() {
  12. // Create a new Channel
  13. ch := make(chan int)
  14. go generate(ch) // Start generate() as a goroutine.
  15. for {
  16. prime :=<-ch // Receive value from channel
  17. fmt.Println(prime)
  18. }
  19. }