Skip to content

Commit 6013a30

Browse files
Using done channel
1 parent 8931c8f commit 6013a30

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func Search(matcher Matcher, searchTerm string, results chan<- Result) {
3333
}
3434

3535
// Display writes results to the console window.
36-
func Display() chan<- Result {
36+
func Display(done chan struct{}) chan<- Result {
3737
// Create a channel to receive the results on.
3838
result := make(chan Result)
3939

@@ -43,6 +43,8 @@ func Display() chan<- Result {
4343
for found := range result {
4444
log.Printf("%s:\n%s\n\n", found.Field, found.Content)
4545
}
46+
47+
done <- struct{}{}
4648
}()
4749

4850
return result

‎src/chapter2/search/main.go‎

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

43+
done := make(chan struct{})
44+
4345
// Start the display routine.
44-
results := search.Display()
46+
results := search.Display(done)
4547

4648
// Setup a wait group so we can process all the feeds.
4749
var waitGroup sync.WaitGroup
@@ -64,4 +66,5 @@ func main() {
6466

6567
// Close the channel and exit.
6668
close(results)
69+
<-done
6770
}

0 commit comments

Comments
 (0)