Skip to content

Instantly share code, notes, and snippets.

@termie
Last active May 5, 2016 20:38
Show Gist options
  • Save termie/f4ab8a1bf89f06129e9a4fcf4f4a40fa to your computer and use it in GitHub Desktop.
Save termie/f4ab8a1bf89f06129e9a4fcf4f4a40fa to your computer and use it in GitHub Desktop.
goroutine with bound or whatever
type Futureproof func (chan error)
func DoSomethingSlow(ctx context.Contex, f Futureproof) errch chan error {
fErr := make(chan error)
go func() {
select {
case <-ctx.Done():
errch <- ctx.Err()
case err := <-fErr:
errch <- err
}
}()
go f(fErr)
return errch
}
func main {
f := func(errch chan error) {
err := makeSomeRequest()
errch <- err
}
ctx := context.WithTimeout(context.Background(), 1*time.Second)
ch := DoSomethingSlow(ctx, f)
// do some other stuff
err <- ch
if err != nil {
log.Panicln("oh noes", err)
}
}
@termie
Copy link
Author

termie commented May 5, 2016

probably needs to close some channels somewhere

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment