Last active
August 29, 2015 14:13
-
-
Save xjdrew/220a39907e98ae415a93 to your computer and use it in GitHub Desktop.
one request one goroutine
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" | |
"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