view src/httpsv/client.go @ 37:43adde439537

ver.up: gf.go
author pyon@macmini
date Tue, 26 Nov 2019 21:04:22 +0900
parents 54a75ff1c288
children
line wrap: on
line source

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 )
}