Sobes.tech
Senior

// Fix the code so that the requests are concurrent and the response code is output // and in case of an error - output its log and continue processing package main import "net/http" var addrs = []string{"[link]", "[link]"} func main() { for _, url := range addrs { resp, err := http.Get(url) if err != nil { log.err return } } }

sobes.tech AI

Answer from AI

go package main

import ( "log" "net/http" "sync" )

var addrs = []string{"https://example.com", "https://golang.org"}

func main() { var wg sync.WaitGroup for _, url := range addrs { wg.Add(1) go func(u string) { defer wg.Done() resp, err := http.Get(u) if err != nil { log.Printf("Error fetching %s: %v", u, err) return } defer resp.Body.Close() log.Printf("Response from %s: %d", u, resp.StatusCode) }(url) } wg.Wait() }


In this corrected version, requests are executed concurrently using goroutines. Errors are logged but do not interrupt the execution of other requests. The response status code is output for each successful request.
// Fix the code so that the requests are concurrent… - sobes.tech