Skip to content

Commit a378788

Browse files
Merge pull request goinaction#15 from gbbr/search
Renaming things and removing constructors
2 parents b3286f7 + 53e02ee commit a378788

2 files changed

Lines changed: 32 additions & 36 deletions

File tree

‎chapter7/patterns/search/search.go‎

Lines changed: 14 additions & 3 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")
30-
s.searchers["google"] = NewGoogle()
35+
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")
35-
s.searchers["bing"] = NewBing()
42+
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")
40-
s.searchers["yahoo"] = NewYahoo()
49+
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: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,92 +18,77 @@ func init() {
1818
// Google provides support for Google searches.
1919
type google struct{}
2020

21-
// NewGoogle returns a Google Searcher value.
22-
func NewGoogle() google {
23-
return google{}
24-
}
25-
2621
// Search implements the Searcher interface. It performs a search
2722
// against Google.
28-
func (g google) Search(searchTerm string, searchResults chan<- []Result) {
29-
log.Printf("Google : Search : Started : searchTerm[%s]\n", searchTerm)
23+
func (g google) Search(term string, results chan<- []Result) {
24+
log.Printf("Google : Search : Started : search term[%s]\n", term)
3025

3126
// Slice for the results.
32-
var results []Result
27+
var r []Result
3328

3429
// Simulate an amount of time for the search.
3530
time.Sleep(time.Millisecond * time.Duration(rand.Int63n(900)))
3631

3732
// Simulate a result for the search.
38-
results = append(results, Result{
33+
r = append(r, Result{
3934
Engine: "Google",
4035
Title: "The Go Programming Language",
4136
Description: "The Go Programming Language",
4237
Link: "https://golang.org/",
4338
})
4439

45-
log.Printf("Google : Search : Completed : Found[%d]\n", len(results))
46-
searchResults <- results
40+
log.Printf("Google : Search : Completed : Found[%d]\n", len(r))
41+
results <- r
4742
}
4843

4944
// Bing provides support for Bing searches.
5045
type bing struct{}
5146

52-
// NewBing returns a Bing Searcher value.
53-
func NewBing() bing {
54-
return bing{}
55-
}
56-
5747
// Search implements the Searcher interface. It performs a search
5848
// against Bing.
59-
func (b bing) Search(searchTerm string, searchResults chan<- []Result) {
60-
log.Printf("Bing : Search : Started : searchTerm[%s]\n", searchTerm)
49+
func (b bing) Search(term string, results chan<- []Result) {
50+
log.Printf("Bing : Search : Started : search term [%s]\n", term)
6151

6252
// Slice for the results.
63-
var results []Result
53+
var r []Result
6454

6555
// Simulate an amount of time for the search.
6656
time.Sleep(time.Millisecond * time.Duration(rand.Int63n(900)))
6757

6858
// Simulate a result for the search.
69-
results = append(results, Result{
59+
r = append(r, Result{
7060
Engine: "Bing",
7161
Title: "A Tour of Go",
7262
Description: "Welcome to a tour of the Go programming language.",
7363
Link: "http://tour.golang.org/",
7464
})
7565

76-
log.Printf("Bing : Search : Completed : Found[%d]\n", len(results))
77-
searchResults <- results
66+
log.Printf("Bing : Search : Completed : Found[%d]\n", len(r))
67+
results <- r
7868
}
7969

8070
// Yahoo provides support for Yahoo searches.
8171
type yahoo struct{}
8272

83-
// NewYahoo returns a Yahoo Searcher value.
84-
func NewYahoo() yahoo {
85-
return yahoo{}
86-
}
87-
8873
// Search implements the Searcher interface. It performs a search
8974
// against Yahoo.
90-
func (y yahoo) Search(searchTerm string, searchResults chan<- []Result) {
91-
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)
9277

9378
// Slice for the results.
94-
var results []Result
79+
var r []Result
9580

9681
// Simulate an amount of time for the search.
9782
time.Sleep(time.Millisecond * time.Duration(rand.Int63n(900)))
9883

9984
// Simulate a result for the search.
100-
results = append(results, Result{
85+
r = append(r, Result{
10186
Engine: "Yahoo",
10287
Title: "Go Playground",
10388
Description: "The Go Playground is a web service that runs on golang.org's servers",
10489
Link: "http://play.golang.org/",
10590
})
10691

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

0 commit comments

Comments
 (0)