我有以下代码:
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,
}
}
你能告诉我哪种方法是实现相同目标但使用单一渠道的正确方法吗?
慕标琳琳
相关分类