Files
toysearch/search.go
T

46 lines
824 B
Go
Raw Normal View History

2013-05-14 20:48:43 -07:00
package main
import (
"fmt"
"math/rand"
"time"
)
var (
Web = fakeSearch("web")
Image = fakeSearch("image")
Video = fakeSearch("video")
)
type Search func(query string) Result
type Result struct {
Goodies string
}
func fakeSearch(kind string) Search {
return func(query string) Result {
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
2013-05-14 20:54:45 -07:00
return Result{fmt.Sprintf("%s result for %q", kind, query)}
2013-05-14 20:48:43 -07:00
}
}
func Google(query string) (results []Result) {
2013-05-14 21:01:26 -07:00
c := make(chan Result)
go func() { c <- Web(query) }()
go func() { c <- Image(query) }()
go func() { c <- Video(query) }()
2013-05-14 20:57:11 -07:00
2013-05-14 21:01:26 -07:00
timeout := time.After(80 * time.Millisecond)
for i := 0; i < 3; i++ {
select {
case result := <-c:
results = append(results, result)
case <-timeout:
fmt.Println("timed out!!")
return
}
}
2013-05-14 20:48:43 -07:00
return
}