comparison src/httpsv/client.go @ 9:54a75ff1c288

New: client.go
author pyon@macmini
date Sun, 20 Nov 2016 16:48:38 +0900
parents
children
comparison
equal deleted inserted replaced
8:bf5d764cd80d 9:54a75ff1c288
1 package main
2
3 import (
4 "fmt"
5 "io/ioutil"
6 "net/http"
7 )
8
9 func main() {
10 ch := make( chan string )
11 var urls []string
12 urls = append( urls, "http://yahoo.com" )
13 urls = append( urls, "http://yahoo.co.jp" )
14
15 for _, url := range urls {
16 go fetch( url, ch )
17 }
18 for range urls {
19 fmt.Println( <-ch )
20 }
21 }
22
23 func fetch( url string, ch chan <- string ) {
24 resp, err := http.Get( url )
25 if err != nil {
26 ch <- fmt.Sprint( err )
27 return
28 }
29 b, err := ioutil.ReadAll( resp.Body )
30 resp.Body.Close()
31 if err != nil {
32 ch <- fmt.Sprint( err )
33 return
34 }
35 ch <- fmt.Sprintf( "%s", b )
36 }
37