I have the following code:
package main
import (
"fmt"
"time"
)
type Response struct {
Data string
Status int
}
func main() {
var rc [10]chan Response
for i := 0; i < 10; i++ {
rc[i] = make(chan Response)
}
var responses []Response
for i := 0; i < 10; i++ {
go func(c chan<- Response, n int) {
c <- GetData(n)
close(c)
}(rc[i], i)
}
for _, resp := range rc {
responses = append(responses, <-resp)
}
for _, item := range responses {
fmt.Printf("%+v\n", item)
}
}
func GetData(n int) Response {
time.Sleep(time.Second * 5)
return Response{
Data: "adfdafcssdf4343t43gf3jn4jknon239nwcwuincs",
Status: n,
}
}
Can you tell me which would be the right way to accomplish the same goal but using a single channel?