I am leaning go, so this might be a stupid question.
I can't seem to figure out why one of my go routines is being blocked by another. My understanding (could be wrong) go routines run as independent light weight threads so they should not be blocking each other, unless I have messed up :)
I have pasted the code below and would appreciate any help / tips to figure this out.
package main
import "fmt"
import "time"
import "sync"
func worker( jobs <-chan int, job2 chan<- int) {
defer wg.Done()
for j := range jobs {
fmt.Println("finished job", j)
time.Sleep(time.Second/2)
if(j%3==0){
job2 <- j
}
}
close(job2)
fmt.Println("channel job2 closed")
}
func worker2(job2 <-chan int) {
defer wg.Done()
for i:= range job2 {
fmt.Println(i)
time.Sleep(time.Second*10)
}
}
var wg sync.WaitGroup
func main() {
wg.Add(2)
jobs := make(chan int)
job2 := make(chan int)
go func() {
for j := 1; j <= 10; j++ {
jobs <- j
}
close(jobs)
fmt.Println("channel jobs closed")
}()
go worker(jobs,job2)
go worker2(job2)
wg.Wait()
fmt.Println("exiting main")
}
I get the following output when I run this code
finished job 1
finished job 2
finished job 3
finished job 4
3
finished job 5
finished job 6
6
finished job 7
finished job 8
finished job 9
9
finished job 10
channel jobs closed
channel job2 closed
exiting main
however I was expecting something like this?
finished job 1
finished job 2
finished job 3
finished job 4
3
finished job 5
finished job 6
finished job 7
finished job 8
finished job 9
finished job 10
channel jobs closed
6
9
channel job2 closed
exiting main