Go Foundations·Practice drills 6

Drill 13: Beginner mistake — Goroutines and channels

Name one mistake a beginner could make with this example.

Beginner 5 min read Lesson 139 of 300 Challenge included
What you will be able to do
  • Name one mistake a beginner could make with this example.

Practice task

Name one mistake a beginner could make with this example.

fetch.goGo
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

Write your answer

Easy

Name one mistake a beginner could make with 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 in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 139 of 300 · 0% of course