Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

41 rindas
791 B

package main
import(
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"time"
)
func main() {
start := time.Now()
ch := make(chan string)
for _, url := range os.Args[1:]{
go fetch(url, ch) // 启动一个goroutine
}
for range os.Args[1:]{
fmt.Println(<- ch)
}
fmt.Printf("IMY*****main* %2fs elapsed\n", time.Since(start).Seconds())
}
func fetch(url string, ch chan <- string){
start := time.Now()
resp, err := http.Get(url)
if err != nil{
ch <- fmt.Sprint(err) // 发送到通道ch
return
}
nbytes, err := io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close() //不要泄露资源
if err != nil{
ch <- fmt.Sprintf("while reading %s %v", url, err)
return
}
secs := time.Since(start).Seconds()
ch <- fmt.Sprintf("IMY***fetch* %.2fs, %7d %s ", secs, nbytes, url)
}