I made this simple script, trying to understand how is channel working, 
somehow if channel "c" is sent after channel "b" is sent the last routine 
is not being proceed,
please see the runner function

package main

import (
     "fmt"
     "strconv"
     "time"
)

func runner(idx int, c chan []int, b chan []int) {
     var temp []int
     fmt.Println("runner " + strconv.Itoa(idx))
     bucket := <-b
     for k, v := range bucket {
          if v != 0 {
               temp = append(temp, v)
               bucket[k] = 0
          }
          if len(temp) == 5 {
               break
          }
     }

     //Strange condition if channel c is sent after channel b is sent,
     //somehow the last runner is not being proceed
     b <- bucket
     c <- temp

     //All runner ara all proceed if c is sent first
  // c <- temp
  // b <- bucket

}

func printer(c chan []int) {
     for {
          select {
          case msg := <-c:
               fmt.Println(msg)
               time.Sleep(time.Second * 1)
          }
     }
}

func main() {

     c := make(chan []int, 5)
     bucket := make(chan []int)

     go runner(1, c, bucket)
     go runner(2, c, bucket)
     go runner(3, c, bucket)
     go runner(4, c, bucket)

     bucket <- []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20}

     go printer(c)

     var input string
     fmt.Scanln(&input)

}


 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to