Junior — Senior
Competitive web page loader
livecode
Task condition
It is necessary to improve the program so that:
- it does not use
time.Sleepto wait for goroutines to finish; - errors that occur are handled correctly;
- it waits for all requests to complete.
package main
import (
"fmt"
"net/http"
"sync"
)
func main() {
urls := []string{
"https://www.lamoda.ru",
"https://www.yandex.ru",
"https://www.mail.ru",
"https://www.google.com",
}
var wg sync.WaitGroup
for _, url := range urls {
wg.Add(1)
go func(url string) {
defer wg.Done()
fmt.Printf("Fetching %s...\n", url)
err := fetchUrl(url)
if err != nil {
fmt.Printf("Error fetching %s: %v\n", url, err)
return
}
fmt.Printf("Fetched %s\n", url)
}(url)
}
fmt.Println("All requests launched!")
wg.Wait()
fmt.Println("Program finished.")
}
func fetchUrl(url string) error {
// Detailed implementation omitted and not related to the task
// _, err := http.Get(url)
return nil
}