Created
January 26, 2019 22:30
-
-
Save keks/983fb17ca3315979fbc9f59d329babcd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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