首页 > 其他 > 详细

4.11 timeout

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

package main

import (
    "fmt"
    "time"
)

func main() {

    to := time.After(3 * time.Second)
    list := make([]string, 0)
    done := make(chan bool, 1)

    fmt.Println("Starting to insert items")
    go func() {
        defer fmt.Println("Exiting goroutine")
        for {
            select {
            case <-to:
                fmt.Println("The time is up")
                done <- true
                return
            default:
                list = append(list, time.Now().String())
            }
        }
    }()

    <-done
    fmt.Printf("Managed to insert %d items\n", len(list))

}

/*

Starting to insert items
The time is up
Exiting goroutine
Managed to insert 1866573 items

*/

4.11 timeout

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

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