33// Package search : search.go manages the searching of results against Google, Yahoo and Bing.
44package 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.
209type 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}
0 commit comments