I have written a dummy code to demonstrate the purpose.
There are 2 channels and 3 goroutines in the code.
1 goroutine is generating numbers based on if they are divisible by 100 with no remainder:
If the number is divisible by 100, it pushes it to the first channel.
Otherwise it pushes it to the second channel.
2 goroutines are the consumers of these channels:
1 goroutine is responsible for consuming the number 1...99 - 101...199 etc.
Other goroutine is responsible for 100, 200, 300 etc.
Now obviously, one goroutine has 99x more work to do than the other goroutine. How is this handled in Go? If a goroutine works more than other, is this goroutine given more CPU time? Or should I handle this situation, for example creating 99 goroutines for the more resource-hungry channel? (for the sake of argument, the jobs are thought of as identical)
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
go generator(ch1, ch2)
go handler(ch1)
go handler2(ch2)
time.Sleep(1*time.Second)
}
func generator(chan1, chan2 chan int){
for i:=0 ; ; i++{
if i%100 == 0{
chan1 <- i
}else{
chan2 <- i
}
}
}
func handler(number chan int){
for _ = range number{
num := <- number
fmt.Println("Number divided by 100 is 0. ", num)
}
}
func handler2(number chan int){
for _ = range number{
num := <- number
fmt.Println("Number divided by 100 is not 0. ", num)
}
}