package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"sync"
"time"
)
var wg sync.WaitGroup
func main() {
start := time.Now()
ch := make(chan string)
for _, url := range os.Args[1:] {
wg.Add(1)
go fetch(url, ch)
}
for range os.Args[1:] {
for i := 0; i < 2; i++ {
fmt.Println(<-ch)
}
}
wg.Wait()
fmt.Printf("%.2fs elasped\n", time.Since(start).Seconds())
}
func fetch(url string, ch chan<- string) {
for i := 0; i < 2; i++ {
start := time.Now()
resp, err := http.Get(url)
if err != nil {
ch <- fmt.Sprint(err)
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("%.2fs %7d %s", secs, nbytes, url)
}
wg.Done()
}
//You need to use waitgroups, your go-routines will execute,
//but your main program exits before its go-routines.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.