Sobes.tech
Senior

How to write parallel HTTP requests in Golang?

sobes.tech AI

Answer from AI

In Go, to perform parallel HTTP requests, goroutines and a channel 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 launched for each URL, which makes an 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.

How to write parallel HTTP requests in Golang? — Golang - sobes.tech