dqroktbn005028 2019-07-17 12:27
浏览 109
已采纳

谁能解释<-done在关闭后将运行的执行情况[关闭]

I can't understand this program for select, so I need help to explain the order of this program,

    done := make(chan interface{})
    go func() {
        time.Sleep(5 * time.Second)
        close(done)
    }()
    workcount := 0
loop:
    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 
", workcount)
  • 写回答

1条回答 默认 最新

  • dongtu0363 2019-07-17 12:44
    关注
        // 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 
    ", workcount)
    

    A much cleaner way to solve this issue would be

    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 
    ", workcount)
                return
            // fired if time.Tick wrote in tick
            case <-tick:
                workcount++
                fmt.Println(workcount)
            }
        }
    }
    
    

    You run your code in main function so we need to return. We gonna fix this code using 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 
    ", 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)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line