分享
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
func Try(f func(), catch func(interface{}), finally func()) {
defer func() {
if finally != nil {
finally()
}
}()
defer func() {
if err := recover(); err != nil {
if catch != nil {
catch(err)
} else {
//loglogic.PFatal(err)//改成你自己的输出代码
}
}
}()
f()
}
如果想开一个新的协程来跑你的方法时
func GoTry(f func(), catch func(interface{}), finally func()) {
go Try(f, catch, finally)
}
如果想做一个管理器,对管理你自己开的所有协程,在你关闭时,可以等他们都退出的时候,再退出
//ThreadGo 子协程管理计数,可以等子协程都完成
//用它来管理所有开的协程,需要等这些线程都跑完
type ThreadGo struct {
Wg sync.WaitGroup //等待
Ctx context.Context
Cal context.CancelFunc
}
func NewThreadGo() *ThreadGo {
reuslt := new(ThreadGo)
reuslt.Ctx, reuslt.Cal = context.WithCancel(context.Background())
return reuslt
}
func (this *ThreadGo) CloseWait() {
this.Cal()
this.Wg.Wait()
}
//Go 在当前线程上跑
func (this *ThreadGo) Go(f func(ctx context.Context)) {
this.Wg.Add(1)
GoTry(func() {
f(this.Ctx)
}, nil, func() {
this.Wg.Done()
})
}
//GoTry 在新协程上跑
func (this *ThreadGo) GoTry(f func(ctx context.Context), catch func(interface{}), finally func()) {
this.Wg.Add(1)
GoTry(
func() {
f(this.Ctx)
},
catch,
func() {
defer this.Wg.Done()
if finally != nil {
finally()
}
})
}
//Try 在当前协程上跑
func (this *ThreadGo) Try(f func(ctx context.Context), catch func(interface{}), finally func()) {
this.Wg.Add(1)
Try(
func() {
f(this.Ctx)
},
catch,
func() {
defer this.Wg.Done()
if finally != nil {
finally()
}
})
}
资源地址:https://github.com/buguang01/gsframe
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信2892 次点击
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
func Try(f func(), catch func(interface{}), finally func()) {
defer func() {
if finally != nil {
finally()
}
}()
defer func() {
if err := recover(); err != nil {
if catch != nil {
catch(err)
} else {
//loglogic.PFatal(err)//改成你自己的输出代码
}
}
}()
f()
}
如果想开一个新的协程来跑你的方法时
func GoTry(f func(), catch func(interface{}), finally func()) {
go Try(f, catch, finally)
}
如果想做一个管理器,对管理你自己开的所有协程,在你关闭时,可以等他们都退出的时候,再退出
//ThreadGo 子协程管理计数,可以等子协程都完成
//用它来管理所有开的协程,需要等这些线程都跑完
type ThreadGo struct {
Wg sync.WaitGroup //等待
Ctx context.Context
Cal context.CancelFunc
}
func NewThreadGo() *ThreadGo {
reuslt := new(ThreadGo)
reuslt.Ctx, reuslt.Cal = context.WithCancel(context.Background())
return reuslt
}
func (this *ThreadGo) CloseWait() {
this.Cal()
this.Wg.Wait()
}
//Go 在当前线程上跑
func (this *ThreadGo) Go(f func(ctx context.Context)) {
this.Wg.Add(1)
GoTry(func() {
f(this.Ctx)
}, nil, func() {
this.Wg.Done()
})
}
//GoTry 在新协程上跑
func (this *ThreadGo) GoTry(f func(ctx context.Context), catch func(interface{}), finally func()) {
this.Wg.Add(1)
GoTry(
func() {
f(this.Ctx)
},
catch,
func() {
defer this.Wg.Done()
if finally != nil {
finally()
}
})
}
//Try 在当前协程上跑
func (this *ThreadGo) Try(f func(ctx context.Context), catch func(interface{}), finally func()) {
this.Wg.Add(1)
Try(
func() {
f(this.Ctx)
},
catch,
func() {
defer this.Wg.Done()
if finally != nil {
finally()
}
})
}