Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*The httpDo function runs the HTTP request and processes its response in a new goroutine. It cancels the request if ctx.Done is closed before the goroutine exits:*/
- func httpDo(ctx context.Context, req *http.Request, f func(*http.Response, error) error) error {
- // Run the HTTP request in a goroutine and pass the response to f.
- c := make(chan error, 1)
- req = req.WithContext(ctx)
- go func() { c <- f(http.DefaultClient.Do(req)) }()
- select {
- case <-ctx.Done():
- <-c // Wait for f to return.
- return ctx.Err()
- case err := <-c:
- return err
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement