当前位置:网站首页>golang 自定义once,当出现error第二次重新设置
golang 自定义once,当出现error第二次重新设置
2022-07-27 15:53:00 【gitxuzan_】
package main
import (
"log"
"sync"
"sync/atomic"
"time"
)
// 一个功能更加强大的Once
type Oncev2 struct {
m sync.Mutex
done uint32
}
// 传入的函数f有返回值error,如果初始化失败,需要返回失败的error
// Do方法会把这个error返回给调用者
func (o *Oncev2) Do(f func() error) error {
if atomic.LoadUint32(&o.done) == 1 {
//fast path
return nil
}
return o.slowDo(f)
}
// 如果还没有初始化
func (o *Oncev2) slowDo(f func() error) error {
o.m.Lock()
defer o.m.Unlock()
var err error
if o.done == 0 {
// 双检查,还没有初始化
err = f()
if err == nil {
// 初始化成功才将标记置为已初始化
atomic.StoreUint32(&o.done, 1)
}
}
return err
}
func main() {
var one = &Oncev2{
}
for{
time.Sleep(time.Second)
err := one.Do(func() error {
log.Println("执行一次")
return nil
//return errors.New("错误")
})
log.Println(err)
}
}
边栏推荐
- 如何开发一款在线Excel表格系统(上)
- 【单片机】2.1 AT89S52单片机的硬件组成
- Taishan Office Technology Lecture: word strange paragraph borders
- Could not obtain transaction-synchronized Session for current thread
- Know things by learning | build a real-time anti plug-in mechanism from 0 to 1 to supplement the offensive and defensive power of mobile games in multiple dimensions
- 面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?
- 细数国产接口协作平台的六把武器!
- 卷积神经网络——SSD论文翻译
- 【cf】#681 A. Kids Seating (Div. 2, based on VK Cup 2019-2020 - Final)
- 微信小程序 实现拨打电话
猜你喜欢

运行loam_velodyne实时建图

JS to realize the right-click menu bar function

Big gap? Requirements and conditions for candidates with different academic qualifications to take the postgraduate entrance examination

An end-to-end file upload program based on form, including client and server

微信小程序 云函数批量删除多条数据 Error: errCode: -502005 database collection not exists

每条你收藏的资讯背后,都离不开TA

知物由学 | 再造巴别塔,我们如何进行NLP跨语言知识迁移?

Are those who are absent from the written examination shortlisted for the teacher recruitment interview? Henan Xiangfu: the statistics of individual candidates' scores are wrong

The Ministry of industry and information technology re governs data security, and Netease Yidun "privacy compliance" keeps the bottom line of enterprise operation

Mlx90640 infrared thermal imager temperature sensor module development notes (VII)
随机推荐
[MCU] 2.2 pin function of AT89S52
【数据库系统概论(王珊)】第11章 并发控制
Can oracle-linux-7.9 support oracle-19c ACFs file system?
PostgreSQL 14 支持winserver2022吗?
灵魂一问:为什么ES比MySQL更适合复杂条件搜索?
Initial polymorphism
如何开发一款在线Excel表格系统(上)
In the fourth week of July, Yidun business risk control focused on the supreme law to regulate app's forcible request for personal information
Helm install kubevela complete makefile script content
【Codeforces】 A. Computer Game
Numpy array matrix operation
初识多态
What are the safety risks of small games?
6月第1周易盾业务风控关注 | 新东方学而思等15家机构被顶格罚款
wallys/DR882-Qualcomm-Atheros-QCA9882-2T2R-MIMO-802.11ac-Mini-PCIe-Wi-Fi-Module-5G-high-power.
Original direct selling MOS tube knl42150 2.8a/1500v applicable photovoltaic inverter can provide samples
MLX90640 红外热成像仪测温传感器模块开发笔记(七)
Fast parsing combined with Huatu document encryption software
快解析结合海典医药
From digitalization to intelligent operation and maintenance: what are the values and challenges?