How can I return a variable?
daviidmart opened this issue · 1 comments
daviidmart commented
I am using the library to make multiple calls and thus save a bit of time in the queries but I cannot find how to return the answered information, example the body of an http request
pieterclaerhout commented
That's not supported out of the box unfortunately but it's quite easy to accomplish. You basically setup a buffered channel with the number of items as the buffer size. After processing each item, just push the results on the channel. Once finished, close the channel and you can loop over the results.
Something like this:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/pieterclaerhout/go-waitgroup"
)
func main() {
urls := []string{
"https://www.easyjet.com/",
"https://www.skyscanner.de/",
"https://www.ryanair.com",
"https://wizzair.com/",
"https://www.swiss.com/",
"http://A",
}
// Create a channel to store the results
results := make(chan string, len(urls))
wg := waitgroup.NewWaitGroup(3)
for _, url := range urls {
urlToCheck := url
wg.Add(func() {
fmt.Printf("%s: checking\n", urlToCheck)
res, err := http.Get(urlToCheck)
if err != nil {
fmt.Printf("Error: %v\n", err)
} else {
defer res.Body.Close()
// Read the body and push it onto the channel
output, _ := ioutil.ReadAll(res.Body)
results <- string(output)
fmt.Printf("%s: result: %v\n", urlToCheck, err)
}
})
}
// Wait for all the items to finish and close the results channel
wg.Wait()
close(results)
fmt.Println("Finished")
// Loop over the channel to get the results
for s := range results {
fmt.Println(s)
}
}