Created
January 5, 2013 01:56
-
-
Save quux00/4459159 to your computer and use it in GitHub Desktop.
Fake google search example in Go using concurrent Go routines pushing to a common Go channel. From Rob Pike's Google IO 2012 presentation: http://www.youtube.com/watch?v=f6kdp27TYZs&feature=youtu.be and adapted from Alexey Kachayev's transcription of Pike's slides: https://gist.github.com/3124594#file-go-channels-7-search-go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main; | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
) | |
var ( | |
Web1 = fakeSearch("web1") | |
Web2 = fakeSearch("web2") | |
Image1 = fakeSearch("image1") | |
Image2 = fakeSearch("image2") | |
Video1 = fakeSearch("video1") | |
Video2 = fakeSearch("video2") | |
) | |
type Result string | |
type Search func(query string) Result | |
func fakeSearch(kind string) Search { | |
return func(query string) Result { | |
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) | |
return Result( fmt.Sprintf("%s result for %q\n", kind, query) ) | |
} | |
} | |
// Replication in order to avoid timeouts | |
func First(query string, replicas ...Search) Result { | |
c := make(chan Result) | |
searchReplica := func(i int) { c <- replicas[i](query) } | |
for i := range replicas { | |
go searchReplica(i) | |
} | |
return <-c | |
} | |
// Main search function | |
func Google(query string) (results []Result) { | |
c := make(chan Result) | |
go func() { c <- First(query, Web1, Web2) }() | |
go func() { c <- First(query, Image1, Image2) }() | |
go func() { c <- First(query, Video1, Video2) }() | |
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 | |
} | |
} | |
return results | |
} | |
func main() { | |
rand.Seed(time.Now().UnixNano()) | |
start := time.Now() | |
results := Google("golang") | |
elapsed := time.Since(start) | |
fmt.Println(results) | |
fmt.Println(elapsed) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also see this: https://github.com/serpapi/google-search-results-golang