小编Tom*_*ard的帖子

Go 例程:发出并发 API 请求

我正在尝试了解通道和 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)

concurrency channel go goroutine

2
推荐指数
2
解决办法
7795
查看次数

标签 统计

channel ×1

concurrency ×1

go ×1

goroutine ×1