Skip to content

Commit 8df3c5f

Browse files
author
gbbr
committed
Initial draft
1 parent 36435e0 commit 8df3c5f

3 files changed

Lines changed: 68 additions & 68 deletions

File tree

‎chapter7/patterns/search/main/main.go‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,27 @@ import (
1313

1414
// main is the entry point for all Go programs.
1515
func main() {
16-
// Define the options we want for this search.
17-
options := search.Options{
18-
SearchTerm: "golang",
19-
Google: true,
20-
Bing: true,
21-
Yahoo: true,
22-
First: true,
23-
}
24-
2516
// Submit the search and display the results.
26-
results := search.Submit(&options)
17+
results := search.Submit(
18+
"golang",
19+
search.First,
20+
search.Google,
21+
search.Bing,
22+
search.Yahoo,
23+
)
24+
2725
for _, result := range results {
2826
log.Printf("main : Results : Info : %+v\n", result)
2927
}
3028

3129
// This time we want to wait for all the results.
32-
options.First = false
33-
results = search.Submit(&options)
30+
results = search.Submit(
31+
"golang",
32+
search.Google,
33+
search.Bing,
34+
search.Yahoo,
35+
)
36+
3437
for _, result := range results {
3538
log.Printf("main : Results : Info : %+v\n", result)
3639
}

‎chapter7/patterns/search/search.go‎

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,7 @@
33
// Package search : search.go manages the searching of results against Google, Yahoo and Bing.
44
package search
55

6-
import (
7-
"log"
8-
)
9-
10-
// Options provides the search options for performing searches.
11-
type Options struct {
12-
SearchTerm string
13-
Google bool
14-
Bing bool
15-
Yahoo bool
16-
First bool
17-
}
6+
import "log"
187

198
// Result represents a search result that was found.
209
type Result struct {
@@ -30,61 +19,69 @@ type Searcher interface {
3019
Search(searchTerm string, searchResults chan<- []Result)
3120
}
3221

33-
// Submit uses goroutines and channels to perform a search against the three
34-
// leading search engines concurrently.
35-
func Submit(options *Options) []Result {
36-
log.Printf("search : Submit : Started : %#v\n", options)
22+
type searchSession struct {
23+
searchers map[string]Searcher
24+
first bool
25+
resultChan chan []Result
26+
}
3727

38-
var final []Result
39-
searchers := make(map[string]Searcher)
40-
searchResults := make(chan []Result)
28+
func Google(s *searchSession) {
29+
log.Println("search : Submit : Info : Adding Google")
30+
s.searchers["google"] = NewGoogle()
31+
}
4132

42-
// Create a Google Searcher if checked.
43-
if options.Google {
44-
log.Println("search : Submit : Info : Adding Google")
45-
searchers["google"] = NewGoogle()
46-
}
33+
func Bing(s *searchSession) {
34+
log.Println("search : Submit : Info : Adding Bing")
35+
s.searchers["bing"] = NewBing()
36+
}
4737

48-
// Create a Bing Searcher if checked.
49-
if options.Bing {
50-
log.Println("search : Submit : Info : Adding Bing")
51-
searchers["bing"] = NewBing()
52-
}
38+
func Yahoo(s *searchSession) {
39+
log.Println("search : Submit : Info : Adding Yahoo")
40+
s.searchers["yahoo"] = NewYahoo()
41+
}
5342

54-
// Create a Yahoo Searcher if checked.
55-
if options.Yahoo {
56-
log.Println("search : Submit : Info : Adding Yahoo")
57-
searchers["yahoo"] = NewYahoo()
43+
func First(s *searchSession) { s.first = true }
44+
45+
// Submit uses goroutines and channels to perform a search against the three
46+
// leading search engines concurrently.
47+
func Submit(query string, options ...func(*searchSession)) []Result {
48+
var session searchSession
49+
session.searchers = make(map[string]Searcher)
50+
session.resultChan = make(chan []Result)
51+
52+
for _, opt := range options {
53+
opt(&session)
5854
}
5955

56+
var result []Result
57+
6058
// Perform the searches concurrently. Using a map because
6159
// it returns the searchers in a random order every time.
62-
for _, searcher := range searchers {
63-
go searcher.Search(options.SearchTerm, searchResults)
60+
for _, s := range session.searchers {
61+
go s.Search(query, session.resultChan)
6462
}
6563

6664
// Wait for the results to come back.
67-
for search := 0; search < len(searchers); search++ {
65+
for search := 0; search < len(session.searchers); search++ {
6866
// If we just want the first result, don't wait any longer by
6967
// concurrently discarding the remaining searchResults.
7068
// Failing to do so will leave the Searchers blocked forever.
71-
if options.First && search > 0 {
69+
if session.first && search > 0 {
7270
go func() {
73-
results := <-searchResults
74-
log.Printf("search : Submit : Info : Results Discarded : Results[%d]\n", len(results))
71+
result = append(result, <-session.resultChan...)
72+
log.Printf("search : Submit : Info : Results Discarded : Results[%d]\n", len(result))
7573
}()
7674
continue
7775
}
7876

7977
// Wait to recieve results.
8078
log.Println("search : Submit : Info : Waiting For Results...")
81-
results := <-searchResults
79+
result = append(result, <-session.resultChan...)
8280

8381
// Save the results to the final slice.
84-
log.Printf("search : Submit : Info : Results Used : Results[%d]\n", len(results))
85-
final = append(final, results...)
82+
log.Printf("search : Submit : Info : Results Used : Results[%d]\n", len(result))
8683
}
8784

88-
log.Printf("search : Submit : Completed : Found [%d] Results\n", len(final))
89-
return final
85+
log.Printf("search : Submit : Completed : Found [%d] Results\n", len(result))
86+
return result
9087
}

‎chapter7/patterns/search/searchers.go‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ func init() {
1616
}
1717

1818
// Google provides support for Google searches.
19-
type Google struct{}
19+
type google struct{}
2020

2121
// NewGoogle returns a Google Searcher value.
22-
func NewGoogle() Google {
23-
return Google{}
22+
func NewGoogle() google {
23+
return google{}
2424
}
2525

2626
// Search implements the Searcher interface. It performs a search
2727
// against Google.
28-
func (g Google) Search(searchTerm string, searchResults chan<- []Result) {
28+
func (g google) Search(searchTerm string, searchResults chan<- []Result) {
2929
log.Printf("Google : Search : Started : searchTerm[%s]\n", searchTerm)
3030

3131
// Slice for the results.
@@ -52,16 +52,16 @@ func (g Google) Search(searchTerm string, searchResults chan<- []Result) {
5252
}
5353

5454
// Bing provides support for Bing searches.
55-
type Bing struct{}
55+
type bing struct{}
5656

5757
// NewBing returns a Bing Searcher value.
58-
func NewBing() Bing {
59-
return Bing{}
58+
func NewBing() bing {
59+
return bing{}
6060
}
6161

6262
// Search implements the Searcher interface. It performs a search
6363
// against Bing.
64-
func (b Bing) Search(searchTerm string, searchResults chan<- []Result) {
64+
func (b bing) Search(searchTerm string, searchResults chan<- []Result) {
6565
log.Printf("Bing : Search : Started : searchTerm[%s]\n", searchTerm)
6666

6767
// Slice for the results.
@@ -88,16 +88,16 @@ func (b Bing) Search(searchTerm string, searchResults chan<- []Result) {
8888
}
8989

9090
// Yahoo provides support for Yahoo searches.
91-
type Yahoo struct{}
91+
type yahoo struct{}
9292

9393
// NewYahoo returns a Yahoo Searcher value.
94-
func NewYahoo() Yahoo {
95-
return Yahoo{}
94+
func NewYahoo() yahoo {
95+
return yahoo{}
9696
}
9797

9898
// Search implements the Searcher interface. It performs a search
9999
// against Yahoo.
100-
func (y Yahoo) Search(searchTerm string, searchResults chan<- []Result) {
100+
func (y yahoo) Search(searchTerm string, searchResults chan<- []Result) {
101101
log.Printf("Yahoo : Search : Started : searchTerm[%s]\n", searchTerm)
102102

103103
// Slice for the results.

0 commit comments

Comments
 (0)