Skip to content

Commit b9bfab3

Browse files
author
gbbr
committed
Added some comments and renamed things
1 parent 92f1785 commit b9bfab3

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

chapter7/patterns/search/search.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,38 @@ type Searcher interface {
1919
Search(searchTerm string, searchResults chan<- []Result)
2020
}
2121

22+
// searchSession holds information about the current search submission.
23+
// It contains options, searchers and a channel down which we will receive
24+
// results.
2225
type searchSession struct {
2326
searchers map[string]Searcher
2427
first bool
2528
resultChan chan []Result
2629
}
2730

31+
// Google search will be added to the search session if this option
32+
// is provided.
2833
func Google(s *searchSession) {
2934
log.Println("search : Submit : Info : Adding Google")
3035
s.searchers["google"] = google{}
3136
}
3237

38+
// Bing search will be added to this search session if this option
39+
// is provided.
3340
func Bing(s *searchSession) {
3441
log.Println("search : Submit : Info : Adding Bing")
3542
s.searchers["bing"] = bing{}
3643
}
3744

45+
// Yahoo search will be enabled if this option is provided as an argument
46+
// to Submit.
3847
func Yahoo(s *searchSession) {
3948
log.Println("search : Submit : Info : Adding Yahoo")
4049
s.searchers["yahoo"] = yahoo{}
4150
}
4251

52+
// OnlyFirst is an option that will restrict the search session to just the
53+
// first result.
4354
func OnlyFirst(s *searchSession) { s.first = true }
4455

4556
// Submit uses goroutines and channels to perform a search against the three

chapter7/patterns/search/searchers.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ type google struct{}
2020

2121
// Search implements the Searcher interface. It performs a search
2222
// against Google.
23-
func (g google) Search(searchTerm string, searchResults chan<- []Result) {
24-
log.Printf("Google : Search : Started : searchTerm[%s]\n", searchTerm)
23+
func (g google) Search(term string, resChan chan<- []Result) {
24+
log.Printf("Google : Search : Started : search term[%s]\n", term)
2525

2626
// Slice for the results.
2727
var results []Result
@@ -38,16 +38,16 @@ func (g google) Search(searchTerm string, searchResults chan<- []Result) {
3838
})
3939

4040
log.Printf("Google : Search : Completed : Found[%d]\n", len(results))
41-
searchResults <- results
41+
resChan <- results
4242
}
4343

4444
// Bing provides support for Bing searches.
4545
type bing struct{}
4646

4747
// Search implements the Searcher interface. It performs a search
4848
// against Bing.
49-
func (b bing) Search(searchTerm string, searchResults chan<- []Result) {
50-
log.Printf("Bing : Search : Started : searchTerm[%s]\n", searchTerm)
49+
func (b bing) Search(term string, resChan chan<- []Result) {
50+
log.Printf("Bing : Search : Started : search term [%s]\n", term)
5151

5252
// Slice for the results.
5353
var results []Result
@@ -64,31 +64,31 @@ func (b bing) Search(searchTerm string, searchResults chan<- []Result) {
6464
})
6565

6666
log.Printf("Bing : Search : Completed : Found[%d]\n", len(results))
67-
searchResults <- results
67+
resChan <- results
6868
}
6969

7070
// Yahoo provides support for Yahoo searches.
7171
type yahoo struct{}
7272

7373
// Search implements the Searcher interface. It performs a search
7474
// against Yahoo.
75-
func (y yahoo) Search(searchTerm string, searchResults chan<- []Result) {
76-
log.Printf("Yahoo : Search : Started : searchTerm[%s]\n", searchTerm)
75+
func (y yahoo) Search(term string, results chan<- []Result) {
76+
log.Printf("Yahoo : Search : Started : search term [%s]\n", term)
7777

7878
// Slice for the results.
79-
var results []Result
79+
var r []Result
8080

8181
// Simulate an amount of time for the search.
8282
time.Sleep(time.Millisecond * time.Duration(rand.Int63n(900)))
8383

8484
// Simulate a result for the search.
85-
results = append(results, Result{
85+
r = append(r, Result{
8686
Engine: "Yahoo",
8787
Title: "Go Playground",
8888
Description: "The Go Playground is a web service that runs on golang.org's servers",
8989
Link: "http://play.golang.org/",
9090
})
9191

92-
log.Printf("Yahoo : Search : Completed : Found[%d]\n", len(results))
93-
searchResults <- results
92+
log.Printf("Yahoo : Search : Completed : Found[%d]\n", len(r))
93+
results <- r
9494
}

0 commit comments

Comments
 (0)