1func fetchWithTimeout(url string, timeout time.Duration) (string, error) {
2 ch := make(chan string)
3 go func() {
4 result := slowFetch(url)
5 ch <- result
6 }()
7
8 select {
9 case res := <-ch:
10 return res, nil
11 case <-time.After(timeout):
12 return "", errors.New("timeout")
13 }
14}
no lines flagged
#067PracticeHard30 min · 200 XP
Goroutine Leak from Blocked Channel Send
Under certain error paths, this function leaks a goroutine forever because nothing ever reads from its result channel.
Flagged linesNo lines flagged yet
What's wrong?
Flag a line or write a note to submit.