Skip to content

Concurrent requests

package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
type post struct {
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
}
func sendRequest(url string, ch chan string) {
resp, error := http.Get(url)
if error != nil {
ch <- "Error while sending request"
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
ch <- "Error while reading body"
}
var p post
json.Unmarshal(body, &p)
ch <- p.Title
}
func main() {
ch := make(chan string)
urls := [3]string{"https://jsonplaceholder.typicode.com/posts/1", "https://jsonplaceholder.typicode.com/posts/2", "https://jsonplaceholder.typicode.com/posts/3"}
for _, url := range urls {
go sendRequest(url, ch)
}
for range urls {
select {
case res := <-ch:
{
fmt.Println(res)
}
}
}
}