golang context简单demo使用

2017-10-27  本文已影响57人  承诺一时的华丽
package test

import (
    "testing"
    "time"
    "context"
)

var (
    ctxt   context.Context
    cancel context.CancelFunc
)

func TestEnum(t *testing.T) {
    ctxt, cancel = context.WithCancel(context.Background())
    go show()

    go func() {
        time.Sleep(time.Second * 10)
        closeShow()
    }()

    time.Sleep(time.Second * 15)
}

func show() {
    for {
        select {
        case <-ctxt.Done():
            println("done")
            return
        default:
            println("work ......")
        }

        time.Sleep(time.Second * 1)
    }
}

func closeShow() {
    println("close 。。。。")
    cancel()
}

上一篇下一篇

猜你喜欢

热点阅读