Skip to content

Instantly share code, notes, and snippets.

@keks
Created January 26, 2019 22:30
Show Gist options
  • Save keks/983fb17ca3315979fbc9f59d329babcd to your computer and use it in GitHub Desktop.
Save keks/983fb17ca3315979fbc9f59d329babcd to your computer and use it in GitHub Desktop.
package errch
import (
"sync"
)
func New() (<-chan error, func() chan<- error) {
var (
src = make(chan error)
wg sync.WaitGroup
)
go func () {
wg.Wait()
close(src)
}()
return src, func() chan<- error{
snk := make(chan error)
wg.Add(1)
go func() {
defer wg.Done()
if err := <-snk; err != nil{
src <- err
}
}()
return snk
}
}
package errch
import (
"testing"
)
func TestErrCh(t *testing.T) {
src, mkSnk := New()
go func() {
mkSnk() <- nil
}()
_, ok := <-src
if ok {
t.Error("expected closed src")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment