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

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

誰能解釋一下 <-done 將在 close [close] 后運(yùn)行的執(zhí)行

誰能解釋一下 <-done 將在 close [close] 后運(yùn)行的執(zhí)行

Go
蠱毒傳說 2023-07-04 16:48:45
我無法理解這個(gè)程序for select,所以我需要幫助來解釋這個(gè)程序的順序,    done := make(chan interface{})    go func() {        time.Sleep(5 * time.Second)        close(done)    }()    workcount := 0loop:    for {        select {        case <-done:            break loop        default:        }        workcount++        fmt.Println(workcount)        time.Sleep(1 * time.Second)    }    fmt.Printf("Achieved %v cycles of work before signalled to stop \n", workcount)
查看完整描述

1 回答

?
慕尼黑5688855

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

// Our communication channel is created

    done := make(chan interface{})


    // run this function as goroutine

    go func() {

        // wait/sleep 5 seconds

        time.Sleep(5 * time.Second)


        // close our communication channel

        close(done)

    }()


    // initialize workcount with 0

    workcount := 0


loop:

    // loop

    for {

        // read channel data

        select {

        // if channel is closed break this loop, break = stop!

        case <-done:

            break loop


        // default do nothing

        default:

        }


        // if our channel doesn't send anything, just add +1 to workcount

        workcount++


        // print workcount

        fmt.Println(workcount)


        // wait 1 second before we run this loop again

        time.Sleep(1 * time.Second)

    }


    // so workcount is 5, cuz our goroutine will send term signal after 5 seconds

    fmt.Printf("Achieved %v cycles of work before signalled to stop \n", workcount)

解決這個(gè)問題的一個(gè)更干凈的方法是


package main


import (

    "fmt"

    "time"

)


func main() {

    // sends after 5 seconds to this channel https://golang.org/pkg/time/#After

    timeout := time.After(5 * time.Second)


    // sends each second to this channel https://golang.org/pkg/time/#Tick

    tick := time.Tick(1 * time.Second)


    // our workcount var

    workcount := 0


    // for infinite

    for {

        // waits for data on each channel

        select {

        // fired if time.After wrote in timeout

        case <-timeout:

            fmt.Printf("Achieved %v cycles of work before signalled to stop \n", workcount)

            return

        // fired if time.Tick wrote in tick

        case <-tick:

            workcount++

            fmt.Println(workcount)

        }

    }

}


您在主函數(shù)中運(yùn)行代碼,因此我們需要返回。我們將使用 return 修復(fù)此代碼


package main


import (

    "fmt"

    "time"

)


func main() {

    // Our communication channel is created

    done := make(chan interface{})


    // run this function as goroutine

    go func() {

        // wait/sleep 5 seconds

        time.Sleep(5 * time.Second)


        // close our communication channel

        close(done)

    }()


    // initialize workcount with 0

    workcount := 0


    // loop

    for {

        // read channel data

        select {

        // if channel is closed break this loop, break = stop!

        case <-done:

            fmt.Printf("Achieved %v cycles of work before signalled to stop \n", workcount)

            return


        // default do nothing

        default:

        }


        // if our channel doesn't send anything, just add +1 to workcount

        workcount++


        // print workcount

        fmt.Println(workcount)


        // wait 1 second before we run this loop again

        time.Sleep(1 * time.Second)

    }

}



查看完整回答
反對(duì) 回復(fù) 2023-07-04
  • 1 回答
  • 0 關(guān)注
  • 229 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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