This is one weird way of doing things. When passing a channel, use `func worker(url chan string) instead.
You should use internal timeout when you perform a network request. This is important because you can never know what kind of error you will bump into and your application should be independent enough to call it off. One good example:
client := http.Client{
Timeout: 5 * time.Second,
}
resp, err := client.Get(url)
...
Your concurrency is unclear and unplanned. Otherwise, you won’t be using global variable. You can try a few things:
- Perform the
http.Client.Get
without concurrency. Do everything onmain
first. - Still maintaining only in
main
, abstract thehttp.Client.Get
into its own function. - Plan your concurrency (Why I receive an intermittent deadlock error?).
Remember, if you can’t handle it in 1 process, don’t bother multiplying in concurrency.