You are doing the resp, err := http.Get(url)
before you initiate the timeout goroutine.
This will cause everything to block until the response is ready, then send on both channels simultaneously.
Just move starting the timeout goroutine to the line before sending the request, and it will be fine. i.e.:
for _, url := range urls {
go func() {
time.Sleep(1 * time.Second)
timeout <- true
count++;
}()
resp, err := http.Get(url)
count++; //I think this is what you meant, right?
ch <- &HttpResponse{url, resp, err}
if err != nil {
return
}
resp.Body.Close()
}
BTW try to use atomic increments to count, and maybe use a waitgroup, and a time.After
channel instead of sleep.