1 Star 0 Fork 0

Wsage/go-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
comag.go 848 Bytes
一键复制 编辑 原始数据 按行查看 历史
Wsage 提交于 3年前 . 优化
package comag
import (
"fmt"
"golang.org/x/net/context"
"time"
)
type CoFunc func(context.Context)
func debugPrint(msg string, params ...interface{}) {
if len(params) <= 0 {
fmt.Println(msg)
return
}
fmt.Printf(msg+"\n", params...)
}
func DemoCoA(ctx context.Context) {
errCh := ctx.Value("error").(chan error)
for i := 1; i <= 100; i++ {
select {
case <-ctx.Done():
debugPrint("input Done by ctx: %s", ctx.Err())
return
default:
if i > 10 {
errCh <- fmt.Errorf("input error")
return
}
debugPrint("input:%d", i)
time.Sleep(time.Second)
}
}
}
func DemoCoB(ctx context.Context) {
for i := 1; i <= 100; i++ {
select {
case <-ctx.Done():
debugPrint("out Done by ctx: %s", ctx.Err())
return
default:
debugPrint("out:%d", i)
time.Sleep(time.Second)
}
}
debugPrint("out Done")
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/scottq/go-framework.git
git@gitee.com:scottq/go-framework.git
scottq
go-framework
go-framework
v1.1.36

搜索帮助