我正在尝试了解通道和 goroutines 并尝试编写一个 goroutine 来向服务器发出并发 API 请求
但是当我使用 goroutine 运行代码时,它似乎花费了与没有 goroutine 相同的时间。
func sendUser(user string, ch chan<- string) {
resp,err := http.get("URL"/user)
//do the processing and get resp=string
ch <- resp
}
func AsyncHTTP(users []string) ([]string, error) {
ch := make(chan string)
var responses []string
var user string
for _ , user = range users {
go sendUser(user, ch)
for {
select {
case r := <-ch:
if r.err != nil {
fmt.Println(r.err)
}
responses = append(responses, r)
**//Is there …Run Code Online (Sandbox Code Playgroud)