Skip to content

Instantly share code, notes, and snippets.

@enjoylife
Created November 29, 2020 04:51
Show Gist options
  • Save enjoylife/847ef74b579525a5555fb20cd229a6d9 to your computer and use it in GitHub Desktop.
Save enjoylife/847ef74b579525a5555fb20cd229a6d9 to your computer and use it in GitHub Desktop.
func withTimeout(ctx context.Context, f func(context.Context) error) error {
c := make(chan error, 1)
go func() { c <- f(ctx) }()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-c:
return err
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment