
go-typedpipe: A Typed, Context-Aware Pipe for Go
Background Go channels are one of the best things about the language. But the moment you need context cancellation, error propagation, and safe concurrent shutdown all at once, a simple chan T starts asking you to write a lot of code just to use it correctly. A common pattern looks something like this: out := make ( chan Result , len ( urls )) errc := make ( chan error , 1 ) go func () { defer close ( out ) for _ , url := range urls { select { case <- ctx . Done () : errc <- ctx . Err () return default : } resp , err := fetch ( ctx , url ) if err != nil { errc <- err return } select { case out <- Result { Data : resp } : case <- ctx . Done () : errc <- ctx . Err () return } } }() This works. But if you're not careful, it's easy to introduce bugs: Double-close panics — closing a channel twice crashes the program Goroutine leaks — the writer forgets to close, readers block forever Lost values — values buffered before close are never read Zombie producers — the writer keeps running after
Continue reading on Dev.to
Opens in a new tab

