Senior
Kuidas kirjutada paralleelsed HTTP-päringud Golangis?
sobes.tech AI
Vastus AI-lt
In Go, to perform parallel HTTP requests, goroutines and channels are typically used to collect results. Example:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func fetch(url string, ch chan<- string) {
resp, err := http.Get(url)
if err != nil {
ch <- fmt.Sprintf("error: %v", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
ch <- fmt.Sprintf("error reading body: %v", err)
return
}
ch <- string(body)
}
func main() {
urls := []string{
"https://example.com",
"https://golang.org",
"https://httpbin.org/get",
}
ch := make(chan string)
for _, url := range urls {
go fetch(url, ch)
}
for range urls {
fmt.Println(<-ch)
}
}
Here, a goroutine is started for each URL, which performs the HTTP request and sends the result to the channel. The main function reads from the channel as many times as there are requests, thus collecting all responses in parallel.