goroutine_close.go 378 B

1234567891011121314151617181920212223242526
  1. // Q20_goroutine.go
  2. package main
  3. import (
  4. "fmt"
  5. )
  6. func tel(ch chan int) {
  7. for i := 0; i < 15; i++ {
  8. ch <- i
  9. }
  10. close(ch) // if this is ommitted: panic: all goroutines are asleep - deadlock!
  11. }
  12. func main() {
  13. var ok = true
  14. var i int
  15. ch := make(chan int)
  16. go tel(ch)
  17. for ok {
  18. if i, ok = <-ch; ok {
  19. fmt.Printf("ok is %t and the counter is at %d\n", ok, i)
  20. }
  21. }
  22. }