Junior — Senior
Stop the program when any URL error occurs
livecode
Task condition
It is necessary to modify the existing application so that when an error occurs from at least one of the requested addresses, all running goroutines stop their work, and the program terminates.
package main
import (
"fmt"
"net/http"
"sync"
"time"
)
func main() {
urls := []string{
"https://www.lamoda.ru",
"https://www.yandex.ru",
"https://www.mail.ru",
"https://www.google.com",
}
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)
}
wg.Wait()
fmt.Println("Program finished.")
}
func fetchUrl(url string) error {
// _, err := http.Get(url)
return nil
}