美文网首页
Golang select语句退出循环

Golang select语句退出循环

作者: FredricZhu | 来源:发表于2019-06-11 09:41 被阅读0次
    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        done := make(chan interface{})
        go func() {
            time.Sleep(5 * time.Second)
            close(done)
        }()
    
        workCounter := 0
    loop:
        for {
            select {
            case <-done:
                break loop
            default:
            }
            workCounter++
            time.Sleep(1 * time.Second)
        }
    
        fmt.Printf("Achieved %v cycles of work before signaled to stop.\n", workCounter)
    }
    
    image.png

    相关文章

      网友评论

          本文标题:Golang select语句退出循环

          本文链接:https://www.haomeiwen.com/subject/uhkffctx.html