August 2016
Beginner to intermediate
665 pages
14h 11m
English
One of the bigger problems in designing something like a pipeline or producer/consumer model is there's somewhat of a black hole when it comes to the state of any given goroutine at any given time.
Consider the following loop, wherein a producer channel creates an arbitrary set of consumer channels and expects each to do one and only one thing:
package main
import (
"fmt"
"time"
)
const CONSUMERS = 5
func main() {
Producer := make(chan (chan int))
for i := 0; i < CONSUMERS; i++ {
go func() {
time.Sleep(1000 * time.Microsecond)
conChan := make(chan int)
go func() {
for {
select {
case _,ok := <-conChan:
if ok {
Producer <- conChan
}else {
return
}
default:
}
}
}()
conChan <- 1
close(conChan)
}()
}Given a random amount ...
Read now
Unlock full access