add example

This commit is contained in:
a 2022-03-26 20:47:22 -05:00
parent b95f29ba03
commit 2edaf2216d

View File

@ -0,0 +1,43 @@
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"git.tuxpa.in/a/lambda/parallel"
)
type someResult struct {
Title string `json:"title"`
Id int `json:"id"`
}
func main() {
var requests = make([]*http.Request, 20)
for i := range requests {
r, _ := http.NewRequest("GET", fmt.Sprintf("https://jsonplaceholder.typicode.com/todos/%d", i), nil)
requests[i] = r
}
results, errs := parallel.MapErrorV(requests, func(r *http.Request) (*someResult, error) {
out := &someResult{}
res, err := http.DefaultClient.Do(r)
if err != nil {
return nil, err
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(out)
return out, err
}, 4)
for i, v := range results {
if errs[i] != nil {
log.Printf("err: %s", errs[i])
} else {
log.Printf("result: %+v", v)
}
}
}