|
| 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