fatal error: all goroutines are asleep - deadlock!

    1. func worker(id int, jobCh <-chan int, sendCh chan<- int) {
    2. for j := range jobCh {
    3. fmt.Printf("worker-%d, start job-%d\n", id, j)
    4. time.Sleep(time.Second * time.Duration(3*j))
    5. fmt.Printf("worker-%d, end job-%d\n", id, j)
    6. sendCh <- j * 2
    7. }
    8. }
    9. func main() {
    10. jobCh := make(chan int, 100)
    11. sendCh := make(chan int, 100)
    12. for w := 1; w <= 3; w++ {
    13. go worker(w, jobCh, sendCh)
    14. }
    15. for j := 1; j <= 3; j++ {
    16. jobCh <- j
    17. }
    18. // sendCh <- 1
    19. //================1====================
    20. // for s := range sendCh {
    21. // fmt.Printf("get %d\n", s)
    22. // }
    23. //================2====================
    24. for a := 1; a <= 4; a++ {
    25. t1 := time.Now().Second()
    26. fmt.Printf("get time %d\n", t1)
    27. s, ok := <-sendCh
    28. if !ok {
    29. break
    30. }
    31. t2 := time.Now().Second()
    32. fmt.Printf("get time %d, %d\n", t2, t1-t2)
    33. fmt.Printf("get %d\n", s)
    34. }
    35. sendCh <- 4 * 2
    36. //================3====================
    37. // for {
    38. // s, ok := <-sendCh
    39. // if !ok {
    40. // fmt.Print(s)
    41. // break
    42. // }
    43. // fmt.Printf("get %d\n", s)
    44. // }
    45. //================4====================
    46. // time.Sleep(time.Second * 5)
    47. // for {
    48. // breakFlag := 0
    49. // select {
    50. // case s := <-sendCh:
    51. // fmt.Printf("get %d\n", s)
    52. // default:
    53. // fmt.Println("channel is empty !")
    54. // breakFlag = 1
    55. // }
    56. // if breakFlag == 1 {
    57. // break
    58. // }
    59. // }
    60. close(jobCh)
    61. close(sendCh)
    62. }