Skip to content

Commit b8fc0a6

Browse files
Fixed exit issue.
1 parent 6013a30 commit b8fc0a6

2 files changed

Lines changed: 17 additions & 24 deletions

File tree

‎src/chapter2/search/feed/feed.go‎

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,10 @@ func Search(matcher Matcher, searchTerm string, results chan<- Result) {
3333
}
3434

3535
// Display writes results to the console window.
36-
func Display(done chan struct{}) chan<- Result {
37-
// Create a channel to receive the results on.
38-
result := make(chan Result)
39-
40-
go func() {
41-
// Wait for results from the different feeds and
42-
// display them.
43-
for found := range result {
44-
log.Printf("%s:\n%s\n\n", found.Field, found.Content)
45-
}
46-
47-
done <- struct{}{}
48-
}()
49-
50-
return result
36+
func Display(results chan Result) {
37+
// Wait for results from the different feeds and
38+
// display them.
39+
for result := range results {
40+
log.Printf("%s:\n%s\n\n", result.Field, result.Content)
41+
}
5142
}

‎src/chapter2/search/main.go‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@ func main() {
4040
log.Fatal(err)
4141
}
4242

43-
done := make(chan struct{})
44-
45-
// Start the display routine.
46-
results := search.Display(done)
43+
// Create a channel to receive the results on.
44+
results := make(chan search.Result)
4745

4846
// Setup a wait group so we can process all the feeds.
4947
var waitGroup sync.WaitGroup
@@ -61,10 +59,14 @@ func main() {
6159
}()
6260
}
6361

64-
// Wait for everything to be processed.
65-
waitGroup.Wait()
62+
go func() {
63+
// Wait for everything to be processed.
64+
waitGroup.Wait()
65+
66+
// Close the channel and exit.
67+
close(results)
68+
}()
6669

67-
// Close the channel and exit.
68-
close(results)
69-
<-done
70+
// Display the results.
71+
search.Display(results)
7072
}

0 commit comments

Comments
 (0)