第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

無法在返回函數(shù)中正確處理錯(cuò)誤

無法在返回函數(shù)中正確處理錯(cuò)誤

Go
qq_花開花謝_0 2022-08-15 10:16:35
該代碼向主機(jī)發(fā)送并行請求,如果我發(fā)送有效的主機(jī),該程序工作正常,但是如果我在執(zhí)行10次后發(fā)送無效主機(jī),則該程序?qū)⑼V构ぷ?。但是這個(gè)程序已經(jīng)在結(jié)構(gòu)中返回錯(cuò)誤。和main函數(shù)處理不正確,我無法找出為什么這個(gè)程序在返回10個(gè)無效結(jié)果后停止。它不會返回 100 個(gè)結(jié)果。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貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊

當(dāng) 觸發(fā)錯(cuò)誤時(shí),您將立即返回,而不會從通道中刪除阻塞項(xiàng)。Get


您可以在此處刪除阻止項(xiàng),也可以刪除并實(shí)際在結(jié)構(gòu)中存儲指向 的指針(這樣就可以從失敗的請求中存儲)。returnhttp.Responsenil


下面是使用第二種方法的 for 循環(huán)的代碼,請注意,狀態(tài)的打印現(xiàn)在處于一個(gè)中,因?yàn)樗赡転?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)

    }


查看完整回答
反對 回復(fù) 2022-08-15
  • 1 回答
  • 0 關(guān)注
  • 93 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號