golang context简单demo使用
作者:
承诺一时的华丽 | 来源:发表于
2017-10-27 14:03 被阅读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()
}
本文标题:golang context简单demo使用
本文链接:https://www.haomeiwen.com/subject/rqwrpxtx.html
网友评论