首页 > 其他 > 详细

3.9 计算器 使用协程

时间:2018-03-22 00:49:22      阅读:246      评论:0      收藏:0      [点我收藏+]

package main

import (
    "fmt"
    "os"
    "os/signal"
    "time"
)

func main() {

    c := make(chan os.Signal, 1)
    signal.Notify(c)

    ticker := time.NewTicker(time.Second)
    stop := make(chan bool)

    go func() {
        defer func() { stop <- true }()
        for {
            select {
            case <-ticker.C:
                fmt.Println("Tick")
            case <-stop:
                fmt.Println("Goroutine closing")
                return
            }
        }
    }()

    // Block until
    // the signal is received
    <-c
    ticker.Stop()

    // Stop the goroutine
    stop <- true
    // Wait until the
    <-stop
    fmt.Println("Application stopped")
}

/*
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Goroutine closing
Application stopped


*/

3.9 计算器 使用协程

原文:https://www.cnblogs.com/zrdpy/p/8620879.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!