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

New: client.go
author pyon@macmini
date Sun, 20 Nov 2016 16:48:38 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/httpsv/client.go	Sun Nov 20 16:48:38 2016 +0900
@@ -0,0 +1,37 @@
+package main
+
+import (
+    "fmt"
+    "io/ioutil"
+    "net/http"
+)
+
+func main() {
+    ch := make( chan string )
+    var urls []string
+    urls = append( urls, "http://yahoo.com" )
+    urls = append( urls, "http://yahoo.co.jp" )
+
+    for _, url := range urls {
+        go fetch( url, ch )
+    }
+    for range urls {
+        fmt.Println( <-ch )
+    }
+}
+
+func fetch( url string, ch chan <- string ) {
+    resp, err := http.Get( url )
+    if err != nil {
+        ch <- fmt.Sprint( err )
+        return
+    }
+    b, err := ioutil.ReadAll( resp.Body )
+    resp.Body.Close()
+    if err != nil {
+        ch <- fmt.Sprint( err )
+        return
+    }
+    ch <- fmt.Sprintf( "%s", b )
+}
+