亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法在返回函數中正確處理錯誤

無法在返回函數中正確處理錯誤

Go
qq_花開花謝_0 2022-08-15 10:16:35
該代碼向主機發送并行請求,如果我發送有效的主機,該程序工作正常,但是如果我在執行10次后發送無效主機,則該程序將停止工作。但是這個程序已經在結構中返回錯誤。和main函數處理不正確,我無法找出為什么這個程序在返回10個無效結果后停止。它不會返回 100 個結果。import (    "fmt"    "net/http"    "sort"    "time"        )// a struct to hold the result from each request including an index// which will be used for sorting the results after they come intype result struct {    index int    res   http.Response    err   error        }// boundedParallelGet sends requests in parallel but only up to a certain// limit, and furthermore it's only parallel up to the amount of CPUs but// is always concurrent up to the concurrency limitfunc boundedParallelGet(urls []string, concurrencyLimit int) []result {    // this buffered channel will block at the concurrency limit    semaphoreChan := make(chan struct{}, concurrencyLimit)    // this channel will not block and collect the http request results    resultsChan := make(chan *result)    // make sure we close these channels when we're done with them    defer func() {        close(semaphoreChan)        close(resultsChan)    }()    // keen an index and loop through every url we will send a request to    for i, url := range urls {        // start a go routine with the index and url in a closure        go func(i int, url string) {            // this sends an empty struct into the semaphoreChan which            // is basically saying add one to the limit, but when the            // limit has been reached block until there is room            semaphoreChan <- struct{}{}            // send the request and put the response in a result struct            // along with the index so we can sort them later along with            // any error that might have occoured            res, err := http.Get(url)                        if err != nil {                            fmt.Println(err)                            return                        }
查看完整描述

1 回答

?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

當 觸發錯誤時,您將立即返回,而不會從通道中刪除阻塞項。Get


您可以在此處刪除阻止項,也可以刪除并實際在結構中存儲指向 的指針(這樣就可以從失敗的請求中存儲)。returnhttp.Responsenil


下面是使用第二種方法的 for 循環的代碼,請注意,狀態的打印現在處于一個中,因為它可能為 nil。else


// a struct to hold a pointer to the result from each request

// including an index which will be used for sorting the

// results after they come in

type result struct {

    index int

    res   *http.Response

    err   error

}


// ...


// keen an index and loop through every url we will send a request to

    for i, url := range urls {


        // start a go routine with the index and url in a closure

        go func(i int, url string) {


            // this sends an empty struct into the semaphoreChan which

            // is basically saying add one to the limit, but when the

            // limit has been reached block until there is room

            semaphoreChan <- struct{}{}


            // send the request and put the response in a result struct

            // along with the index so we can sort them later along with

            // any error that might have occoured

            res, err := http.Get(url)

            if err != nil {

                fmt.Println(err)

            } else {

                fmt.Println(res.Status)

            }


            result := &result{i, res, err}


            // now we can send the result struct through the resultsChan

            resultsChan <- result


            // once we're done it's we read from the semaphoreChan which

            // has the effect of removing one from the limit and allowing

            // another goroutine to start

            <-semaphoreChan


        }(i, url)

    }


查看完整回答
反對 回復 2022-08-15
  • 1 回答
  • 0 關注
  • 91 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號