Skip to content

Instantly share code, notes, and snippets.

@xjdrew
Last active August 29, 2015 14:13
Show Gist options
  • Save xjdrew/220a39907e98ae415a93 to your computer and use it in GitHub Desktop.
Save xjdrew/220a39907e98ae415a93 to your computer and use it in GitHub Desktop.
one request one goroutine
package main
import (
"fmt"
"net/http"
)
type Response struct {
url string
result string
}
func worker(url string, respCh chan<- Response) {
resp, err := http.Head(url)
if err != nil {
respCh <- Response{url, err.Error()}
} else {
respCh <- Response{url, resp.Status}
}
}
func main() {
urls := []string{
"http://www.maybe-no-web.com/",
"http://www.goole.com/",
"http://www.yahoo.com/",
"http://www.baidu.com/",
"http://www.ejoy.com/",
"http://www.441a.com/",
}
respCh := make(chan Response)
for _, url := range urls {
go worker(url, respCh)
}
for _ = range urls {
resp := <-respCh
fmt.Printf("%s -> %s\n", resp.url, resp.result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment