funny enough, in Go is a little bit harder to do the exactly same!
i recommend you to go through two blog posts:
you will need to use channels/goroutines to achieve the same.. as said by rhymes in this thread, you will need to use WaitGroup!
there's a merge function in the documentation link of item 1 above, where using it, you can do something like this:
merge
func makeRequest(url string) <-chan string { res := make(chan string) go func() { res <- "Request started..." _, err := http.Get(url) if err != nil { res <- "Failed." } res <- "Done!" close(res) }() return res } func main() { c1 := makeRequest("https://google.com/") c2 := makeRequest("https://twitter.com/") for n := range merge(c1, c2) { fmt.Println(n) } }
running it will print:
$ go run main.go Request started... Request started... Done! Done!
i've been using it since I discovered it :)
I think you can also use errgroup to handle errors gracefully
This looks simple and powerful. Probably I'll have to dig deeper to understand it. Thanks for the resources.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
funny enough, in Go is a little bit harder to do the exactly same!
i recommend you to go through two blog posts:
you will need to use channels/goroutines to achieve the same.. as said by rhymes in this thread, you will need to use WaitGroup!
there's a
mergefunction in the documentation link of item 1 above, where using it, you can do something like this:running it will print:
i've been using it since I discovered it :)
I think you can also use errgroup to handle errors gracefully
This looks simple and powerful. Probably I'll have to dig deeper to understand it. Thanks for the resources.