Sobes.tech
Middle+

How to write parallel HTTP requests in Go?

sobes.tech AI

Answer from AI

To perform parallel HTTP requests in Go, goroutines and sync.WaitGroup are typically used to wait for all requests to complete.

Example:

package main

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

func fetch(url string, wg *sync.WaitGroup) {
    defer wg.Done()
    resp, err := http.Get(url)
    if err != nil {
        fmt.Printf("Request error to %s: %v\n", url, err)
        return
    }
    defer resp.Body.Close()
    fmt.Printf("Response from %s: %s\n", url, resp.Status)
}

func main() {
    urls := []string{
        "https://golang.org",
        "https://google.com",
        "https://github.com",
    }

    var wg sync.WaitGroup
    wg.Add(len(urls))

    for _, url := range urls {
        go fetch(url, &wg)
    }

    wg.Wait()
    fmt.Println("All requests completed")
}

In this example, a separate goroutine is launched for each URL to perform the HTTP request. WaitGroup ensures that the program waits for all requests to finish before exiting.