Drill 6: Trade-off — Goroutines and channels
Explain the trade-off in this example.
What you will be able to do
- Explain the trade-off in this example.
Practice task
Explain the trade-off in this example.
func FetchAll(ctx context.Context, urls []string) ([]string, error) {
results := make(chan string, len(urls))
var group sync.WaitGroup
for _, url := range urls {
group.Add(1)
go func(url string) {
defer group.Done()
body, err := fetch(ctx, url)
if err == nil {
results <- body
}
}(url)
}
group.Wait()
close(results)
var bodies []string
for body := range results {
bodies = append(bodies, body)
}
return bodies, nil
}Your turn
EasyWrite your answer
Explain the trade-off in this example.
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.