Solution: Timing HTTP calls
- [Instructor] So, we are going to make this MultiURLTime run concurrently, and, ideally, I'd like just to do go URLTime, and then everything runs in a goroutine. The problem is that the Go runtime does not wait for goroutines. We need to wait for them. Because we don't actually need any answers from the goroutine, the thing that we want to use here is a WaitGroup. So, var wg sync.WaitGroup. And, we need to initialize the WaitGroup with how many jobs there are. And this is the len of the urls. So wg.Add (len(urls)). Okay, so now we have a sync. And then at the end what we need to do, oops, after we do the follow-up, is we need to do wg.Wait but we need to notify when a task is done. And this is where we need to actually write our own function, which is in our case, an anonymous function to do that, okay? But if you're going to use that, this URL is going to be repeated for all of the goroutines, the closer capture. So I'm going to create a local variable for the follow-up. And this way the goroutine will accept it's own URL. They're not going to run the same URL everyone. And finally defer wg.Done signaling that there are no more URLs. Okay, so let's run. Test my code. Oh, we need to import the sync package. So we import the sync package and now we can run, Test my code. And now it works. And we see that even though we have 200, 100 and 53, the total time is 203, roughly the last one or the longest one.