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

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

Golang 頻道問(wèn)題

Golang 頻道問(wèn)題

Go
有只小跳蛙 2022-05-23 18:08:21
我有一個(gè)簡(jiǎn)單的頻道示例:https: //play.golang.org/p/eLcpzXeCHmspackage mainimport (    "fmt")func execute(trueChan chan<- bool, lowerRange int32, upperRange int32) {    for lowerRange <= upperRange {        fmt.Printf("\nhandling number %v", lowerRange)        if lowerRange%2 == 0 {            go func() {                trueChan <- true            }()        }        lowerRange++    }    close(trueChan)}func main() {    counter := 0    trueChan := make(chan bool)    execute(trueChan, 5, 25)    for {        if _, ok := <-trueChan; ok {            counter++        } else {            break        }    }    fmt.Printf("\n%v", counter)}第一個(gè)問(wèn)題:我有時(shí)會(huì)收到一條錯(cuò)誤消息...handling number 5handling number 6handling number 7handling number 8handling number 9handling number 10handling number 11handling number 12handling number 13handling number 14handling number 15handling number 16handling number 17handling number 18handling number 19handling number 20handling number 21handling number 22handling number 23handling number 24handling number 250panic: send on closed channel第二個(gè)問(wèn)題 - 我的計(jì)數(shù)器始終為 0。有人可以給我一個(gè)提示,我做錯(cuò)了什么?
查看完整描述

2 回答

?
米琪卡哇伊

TA貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個(gè)贊

你的代碼:

  1. 創(chuàng)建一個(gè)無(wú)緩沖的通道trueChan

  2. 創(chuàng)建 10 個(gè) goroutine,每個(gè) goroutine 都將嘗試寫入trueChan,這將阻塞直到有東西從中讀取。

  3. 關(guān)閉trueChan,然后返回main()

  4. main()打印0,因?yàn)樗€沒(méi)有從 goroutine 中讀取任何內(nèi)容

  5. 同時(shí),由于trueChan在步驟 3 中關(guān)閉,在 goroutine 完成使用它之前,第一個(gè)嘗試寫入通道的 goroutine 出現(xiàn)恐慌

至少,在trueChan您知道所有 goroutine 都已完成之前,您不應(yīng)該關(guān)閉它。實(shí)際上,您甚至在他們開(kāi)始使用它之前就將其關(guān)閉。

Async.WaitGroup可能是做到這一點(diǎn)的一種方法,但在你的代碼中如何做到這一點(diǎn)并不明顯,因?yàn)槲也煌耆_定你的目標(biāo)。這段代碼看起來(lái)像一個(gè)簡(jiǎn)單的練習(xí),而不是一個(gè)真實(shí)的例子。如果你能解釋你的目標(biāo),我可能會(huì)提供更具體的建議。


查看完整回答
反對(duì) 回復(fù) 2022-05-23
?
慕虎7371278

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊

您的第一個(gè)和第二個(gè)問(wèn)題在同一個(gè)根源:

您在主程序中關(guān)閉通道,程序關(guān)閉通道,在您的例程將數(shù)據(jù)發(fā)送到通道之前退出程序您

通常必須在使用該程序的延遲中關(guān)閉通道香奈兒。例如,對(duì)于 yout 代碼的修復(fù):


package main


import (

    "fmt"

)


func execute(trueChan chan<- bool, lowerRange int32, upperRange int32) {

    go func() {

        defer func(){

            close (trueChan)

        }()

        for lowerRange <= upperRange {

            fmt.Printf("\n handling number %v", lowerRange)

            if lowerRange%2 == 0 {

                trueChan <- true

            }

            lowerRange++

        }


    }()

}


func main() {

    counter := 0


    trueChan := make(chan bool)


    execute(trueChan, 5, 25)


    for _ = range trueChan{ // For small improvement here. Ref as below 

            counter++

    }


    fmt.Printf("\n%v", counter)

}


https://tour.golang.org/concurrency/4


查看完整回答
反對(duì) 回復(fù) 2022-05-23
  • 2 回答
  • 0 關(guān)注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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