-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (50 loc) · 1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"time"
"math/rand"
)
/*
Building on the synchronous google search. All options are
asynchronously fetched and presented as one.
*/
type Result string
type Search func(kind string) Result
var (
Web = doSearch("web")
Image = doSearch("image")
Video = doSearch("video")
Shopping = doSearch("shopping")
)
func doSearch(query string) Search {
return func(kind string) Result {
time.Sleep(time.Duration(rand.Intn(2)) * time.Second)
return Result(fmt.Sprintf("result %s for query %q\n", query, kind))
}
}
func GoogleSearch(query string) (results []Result) {
ch := make(chan Result, 4)
go func() {
ch <- Web(query)
}()
go func() {
ch <- Image(query)
}()
go func() {
ch <- Video(query)
}()
go func() {
ch <- Shopping(query)
}()
for i := 0; i < 4; i++ {
results = append(results, <-ch)
}
return
}
func main() {
now := time.Now()
results := GoogleSearch("concert tickets")
elapsed := time.Since(now)
fmt.Println(results)
fmt.Println(elapsed)
}