Implementing nil channel blocks
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 ...
Get Go: Building Web Applications now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.