为了账号安全,请及时绑定邮箱和手机立即绑定

谁能解释一下 <-done 将在 close [close] 后运行的执行

谁能解释一下 <-done 将在 close [close] 后运行的执行

Go
蛊毒传说 2023-07-04 16:48:45
我无法理解这个程序for select,所以我需要帮助来解释这个程序的顺序,    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贡献1848条经验 获得超2个赞

// 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)

解决这个问题的一个更干净的方法是


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)

        }

    }

}


您在主函数中运行代码,因此我们需要返回。我们将使用 return 修复此代码


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)

    }

}



查看完整回答
反对 回复 2023-07-04
  • 1 回答
  • 0 关注
  • 131 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信