Home > Blockchain >  How to make each worker work with Map Channel?
How to make each worker work with Map Channel?

Time:01-04

My Snippet code:

Main Worker:

func Workergrab(m map[int][][]string, ch chan [][]string, wg *sync.WaitGroup) {
    for y := 1993; y <= 2014; y   {
        ch <- m[y]
    }
    wg.Done()
}

Worker1:

fmt.Println(<-ch)

Ok here was what i want to do

-> Main-Worker grabbing map data from m[y] and send it over to channel
-> Worker1 do get data from channel that main worker send at same time depend on y

I already tryed above, worker1 only show me m[1993] data. Messed

I was wondering using map channel, but not sure how make map channel. I think messed up in channel thing..., I wonder how i can make channel of map[int][][]string so my worker1 will catch data from channel for each ID "1993", "1994" etc...

CodePudding user response:

Running your code throw all goroutines are asleep - deadlock! error; It means both of goroutines are ended or waited on writing/reading part forever.

Workergrab tries to send more than one value to the ch channel but Worker1 only prints one ch value and finishes its work. So changing Worker1 as the following can solve the problem:

func Worker1(ch chan [][]string) {
    for {
        fmt.Println(<-ch)
    }
}
  •  Tags:  
  • Related