Drill 15: Find the core line — Context and timeouts
Point to the line that does the main work and explain why.
What you will be able to do
- Point to the line that does the main work and explain why.
Practice task
Point to the line that does the main work and explain why.
package main
import (
"context"
"fmt"
"net/http"
"time"
)
func fetch(ctx context.Context, url string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
fmt.Println(url, resp.StatusCode)
return nil
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := fetch(ctx, "https://example.com"); err != nil {
fmt.Println(err)
}
}Your turn
EasyWrite your answer
Point to the line that does the main work and explain why.
TEXT · starter
Your answer…Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.