What's the issue with this code

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:

  1. Perform the http.Client.Get without concurrency. Do everything on main first.
  2. Still maintaining only in main, abstract the http.Client.Get into its own function.
  3. 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.

1 Like