/
main.go
36 lines (30 loc) · 989 Bytes
/
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
/*
In this example, an array of web URL's are iterated upon and passed
individually to separate goroutines. Each goroutine executes asyncronously and
queries the passed URL. Each query response is passed into the first channel,
which (of course) ensures the first query to respond is the first passed into
the channel. We can then read this response from the channel and act
accordingly.
*/
package main
import "net/http"
type response struct {
resp *http.Response
url string
}
func get(url string, r chan response) {
if resp, err := http.Get(url); err == nil {
r <- response{resp, url}
}
}
func main() {
first := make(chan response)
for _, url := range []string{"http://code.jquery.com/jquery-1.9.1.min.js",
"http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js",
"http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js",
"http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"} {
go get(url, first)
}
r := <-first
// ... do something
}