当前位置:网站首页>Golang customize once. When error occurs, reset it for the second time
Golang customize once. When error occurs, reset it for the second time
2022-07-27 18:10:00 【gitxuzan_】
package main
import (
"log"
"sync"
"sync/atomic"
"time"
)
// A more powerful Once
type Oncev2 struct {
m sync.Mutex
done uint32
}
// Function passed in f There is a return value error, If initialization fails , You need to return the failed error
// Do The method will put this error Back to the caller
func (o *Oncev2) Do(f func() error) error {
if atomic.LoadUint32(&o.done) == 1 {
//fast path
return nil
}
return o.slowDo(f)
}
// If it's not already initialized
func (o *Oncev2) slowDo(f func() error) error {
o.m.Lock()
defer o.m.Unlock()
var err error
if o.done == 0 {
// Double check , It's not initialized yet
err = f()
if err == nil {
// The flag is set to initialized only after initialization is successful
atomic.StoreUint32(&o.done, 1)
}
}
return err
}
func main() {
var one = &Oncev2{
}
for{
time.Sleep(time.Second)
err := one.Do(func() error {
log.Println(" Do it once ")
return nil
//return errors.New(" error ")
})
log.Println(err)
}
}
边栏推荐
- ES查询限制10000条数据解决方法
- MySql代码数据库创建 停车管理系统 外键
- How can we carry out NLP cross language knowledge transfer?
- 用slmgr命令激活正版Win7旗舰版系统
- Application of knowing things and learning | correlation graph analysis in anti cheating business
- 备份表恢复表
- Knowing things by learning | app slimming down, the way of safety reinforcement under the new generation AAB framework
- What's the use of games| Game application value research case collection
- Bug records using distributed framework WCF
- Evaluation index of machine learning (II) -- classification evaluation index
猜你喜欢

美团二面:为什么Redis会有哨兵?

Behind every piece of information you collect, you can't live without TA

机器学习——概念理解之IoU

Convolutional neural network -- SSD thesis translation

知物由学 | 易盾自研文本实时聚类技术,一网打尽社交网络中的同类有害内容

卷积神经网络之卷积计算过程个人理解

I got the P8 "top-level" distributed architecture manual crazy spread on Alibaba intranet

How difficult the interview is! I was forced to survive the six rounds of interview of ant financial! Almost out (interview resumption)

In the fourth week of July, Yidun business risk control focused on the supreme law to regulate app's forcible request for personal information

Salesforce certified sharing and visibility Designer (su20) certification examination summary
随机推荐
golang 等待一组goroutine完成,并带返回值(2)
golang 并发缓存击穿或合并请求
Oracle 11g database installation tutorial
golang 自定义once,当出现error第二次重新设置
知物由学 | 关联图分析在反作弊业务中的应用
机器学习之评价指标(一)——回归评价指标
Code compliance: five reasons why developers use helix QAC
2022 safety officer-c certificate special operation certificate examination question bank and answers
施耐德电气、欧莱雅等企业巨头如何开放式创新?DEMO WORLD世界创新峰会揭秘
Behind every piece of information you collect, you can't live without TA
JS中的冒泡排序
知物由学 | 小游戏的安全风险在哪里?
知物由学 | 易盾移动端同构实践,几步改善官网交互体验
用slmgr命令激活正版Win7旗舰版系统
WPF做登陆界面
Detailed explanation of browser caching mechanism
类的六大关系——依赖和关联的区别
Understand JVM language
Big gap? Requirements and conditions for candidates with different academic qualifications to take the postgraduate entrance examination
JSP custom tag (bottom)