Skip to content

Commit fb299db

Browse files
Search patterns for the book.
1 parent 25bf70f commit fb299db

3 files changed

Lines changed: 251 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright Information.
2+
3+
// This sample program demonstrates how to implement a pattern for
4+
// concurrent requesting results from different systems and either
5+
// wait for all the results to return or just the first one.
6+
package main
7+
8+
import (
9+
"log"
10+
11+
"github.com/goinaction/code/chapter7/patterns/search"
12+
)
13+
14+
// main is the entry point for all Go programs.
15+
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+
25+
// Submit the search and display the results.
26+
results := search.Submit(&options)
27+
for _, result := range results {
28+
log.Printf("main : Results : Info : %+v\n", result)
29+
}
30+
31+
// This time we want to wait for all the results.
32+
options.First = false
33+
results = search.Submit(&options)
34+
for _, result := range results {
35+
log.Printf("main : Results : Info : %+v\n", result)
36+
}
37+
}

‎chapter7/patterns/search/search.go‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright Information.
2+
3+
// Package search : search.go manages the searching of results against Google, Yahoo and Bing.
4+
package search
5+
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+
}
18+
19+
// Result represents a search result that was found.
20+
type Result struct {
21+
Engine string
22+
Title string
23+
Description string
24+
Link string
25+
}
26+
27+
// Searcher declares an interface used to leverage different
28+
// search engines to find results.
29+
type Searcher interface {
30+
Search(searchTerm string, searchResults chan<- []Result)
31+
}
32+
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)
37+
38+
var final []Result
39+
searchers := make(map[string]Searcher)
40+
searchResults := make(chan []Result)
41+
42+
// Create a Google Searcher if checked.
43+
if options.Google {
44+
log.Println("search : Submit : Info : Adding Google")
45+
searchers["google"] = NewGoogle()
46+
}
47+
48+
// Create a Bing Searcher if checked.
49+
if options.Bing {
50+
log.Println("search : Submit : Info : Adding Bing")
51+
searchers["bing"] = NewBing()
52+
}
53+
54+
// Create a Yahoo Searcher if checked.
55+
if options.Yahoo {
56+
log.Println("search : Submit : Info : Adding Yahoo")
57+
searchers["yahoo"] = NewYahoo()
58+
}
59+
60+
// Perform the searches concurrently. Using a map because
61+
// it returns the searchers in a random order every time.
62+
for _, searcher := range searchers {
63+
go searcher.Search(options.SearchTerm, searchResults)
64+
}
65+
66+
// Wait for the results to come back.
67+
for search := 0; search < len(searchers); search++ {
68+
// If we just want the first result, don't wait any longer by
69+
// concurrently discarding the remaining searchResults.
70+
// Failing to do so will leave the Searchers blocked forever.
71+
if options.First && search > 0 {
72+
go func() {
73+
results := <-searchResults
74+
log.Printf("search : Submit : Info : Results Discarded : Results[%d]\n", len(results))
75+
}()
76+
continue
77+
}
78+
79+
// Wait to recieve results.
80+
log.Println("search : Submit : Info : Waiting For Results...")
81+
results := <-searchResults
82+
83+
// 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...)
86+
}
87+
88+
log.Printf("search : Submit : Completed : Found [%d] Results\n", len(final))
89+
return final
90+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Copyright Information.
2+
3+
// Package search : seachers.go contains all the different implementations
4+
// for the existing searchers.
5+
package search
6+
7+
import (
8+
"log"
9+
"math/rand"
10+
"time"
11+
)
12+
13+
// init is called before main.
14+
func init() {
15+
rand.Seed(time.Now().UnixNano())
16+
}
17+
18+
// Google provides support for Google searches.
19+
type Google struct{}
20+
21+
// NewGoogle returns a Google Searcher value.
22+
func NewGoogle() Searcher {
23+
return Google{}
24+
}
25+
26+
// Search implements the Searcher interface. It performs a search
27+
// against Google.
28+
func (g Google) Search(searchTerm string, searchResults chan<- []Result) {
29+
log.Printf("Google : Search : Started : searchTerm[%s]\n", searchTerm)
30+
31+
// Slice for the results.
32+
var results []Result
33+
34+
// On return send the results we have.
35+
defer func() {
36+
log.Println("Google : Search : Info : Sending Results")
37+
searchResults <- results
38+
}()
39+
40+
// Simulate an amount of time for the search.
41+
time.Sleep(time.Millisecond * time.Duration(rand.Int63n(900)))
42+
43+
// Simulate a result for the search.
44+
results = append(results, Result{
45+
Engine: "Google",
46+
Title: "The Go Programming Language",
47+
Description: "The Go Programming Language",
48+
Link: "https://golang.org/",
49+
})
50+
51+
log.Printf("Google : Search : Completed : Found[%d]\n", len(results))
52+
}
53+
54+
// Bing provides support for Bing searches.
55+
type Bing struct{}
56+
57+
// NewBing returns a Bing Searcher value.
58+
func NewBing() Searcher {
59+
return Bing{}
60+
}
61+
62+
// Search implements the Searcher interface. It performs a search
63+
// against Bing.
64+
func (b Bing) Search(searchTerm string, searchResults chan<- []Result) {
65+
log.Printf("Bing : Search : Started : searchTerm[%s]\n", searchTerm)
66+
67+
// Slice for the results.
68+
var results []Result
69+
70+
// On return send the results we have.
71+
defer func() {
72+
log.Println("Bing : Search : Info : Sending Results")
73+
searchResults <- results
74+
}()
75+
76+
// Simulate an amount of time for the search.
77+
time.Sleep(time.Millisecond * time.Duration(rand.Int63n(900)))
78+
79+
// Simulate a result for the search.
80+
results = append(results, Result{
81+
Engine: "Bing",
82+
Title: "A Tour of Go",
83+
Description: "Welcome to a tour of the Go programming language.",
84+
Link: "http://tour.golang.org/",
85+
})
86+
87+
log.Printf("Bing : Search : Completed : Found[%d]\n", len(results))
88+
}
89+
90+
// Yahoo provides support for Yahoo searches.
91+
type Yahoo struct{}
92+
93+
// NewYahoo returns a Yahoo Searcher value.
94+
func NewYahoo() Searcher {
95+
return Yahoo{}
96+
}
97+
98+
// Search implements the Searcher interface. It performs a search
99+
// against Yahoo.
100+
func (y Yahoo) Search(searchTerm string, searchResults chan<- []Result) {
101+
log.Printf("Yahoo : Search : Started : searchTerm[%s]\n", searchTerm)
102+
103+
// Slice for the results.
104+
var results []Result
105+
106+
// On return send the results we have.
107+
defer func() {
108+
log.Println("Yahoo : Search : Info : Sending Results")
109+
searchResults <- results
110+
}()
111+
112+
// Simulate an amount of time for the search.
113+
time.Sleep(time.Millisecond * time.Duration(rand.Int63n(900)))
114+
115+
// Simulate a result for the search.
116+
results = append(results, Result{
117+
Engine: "Yahoo",
118+
Title: "Go Playground",
119+
Description: "The Go Playground is a web service that runs on golang.org's servers",
120+
Link: "http://play.golang.org/",
121+
})
122+
123+
log.Printf("Yahoo : Search : Completed : Found[%d]\n", len(results))
124+
}

0 commit comments

Comments
 (0)