当前位置: 首页 > news >正文

2345系统导航长沙关键词优化费用

2345系统导航,长沙关键词优化费用,网站建设交流,建设部网站怎么查岗位人员GO系列 1、GO学习之Hello World 2、GO学习之入门语法 3、GO学习之切片操作 4、GO学习之 Map 操作 5、GO学习之 结构体 操作 6、GO学习之 通道(Channel) 7、GO学习之 多线程(goroutine) 8、GO学习之 函数(Function) 9、GO学习之 接口(Interface) 10、GO学习之 网络通信(Net/Htt…

GO系列

1、GO学习之Hello World
2、GO学习之入门语法
3、GO学习之切片操作
4、GO学习之 Map 操作
5、GO学习之 结构体 操作
6、GO学习之 通道(Channel)
7、GO学习之 多线程(goroutine)
8、GO学习之 函数(Function)
9、GO学习之 接口(Interface)
10、GO学习之 网络通信(Net/Http)
11、GO学习之 微框架(Gin)
12、GO学习之 数据库(mysql)
13、GO学习之 数据库(Redis)
14、GO学习之 搜索引擎(ElasticSearch)
15、GO学习之 消息队列(Kafka)
16、GO学习之 远程过程调用(RPC)
17、GO学习之 goroutine的调度原理
18、GO学习之 通道(nil Channel妙用)
19、GO学习之 同步操作sync包
20、GO学习之 互斥锁、读写锁该如何取舍
21、GO学习之 条件变量 sync.Cond
22、GO学习之 单例模式 sync.Once

文章目录

  • GO系列
  • 前言
  • sync.Once 如何使用

前言

按照公司目前的任务,go 学习是必经之路了,虽然行业卷,不过技多不压身,依旧努力!!!
sync.Once又是什么呢?某个 goroutine 只需要且只能执行一次,到目前为止我们知道程序运行期间只被执行一次的就是每个包的 init 函数,sync包也提供了这样一种更灵活的机制,可以保证 任意一个函数 在程序运行期间只被执行一次,就是 sync.Once了。

sync.Once 如何使用

在 JAVA 开发中,Spring 框架使用了 单例模式 来创建实体 Bean,我们在开发中也是多用单例模式来避免实例的重复创建。
在 Go 标准库中,sync.Once 的 “仅仅执行一次” 的特性被多用于初始化操作或者是资源清理的过程中,以避免重复执行导致性能不是最佳。
sync.Once 的语义十分适合实现单例模式(singleton)模式,并且实现起来也和简单,如下获取实例的例子:

package mainimport ("log""sync""time"
)// 定义一个结构体对象
type Entity struct{}var once sync.Once// 声明一个接口实例,用于存放实例
var instance *Entityfunc GetFun(id int) *Entity {// 延迟获取异常信息defer func() {e := recover()if e != nil {log.Printf("goroutine %d catch a panic: %s \n", id, e)}}()log.Printf("goroutine %d start run... \n", id)// once.Do 真正的执行创建实体操作once.Do(func() {instance = &Entity{}time.Sleep(3 * time.Second)log.Printf("goroutine %d create instance : %p\n", id, instance)panic("success to create a instance")})return instance
}func main() {var wg sync.WaitGroupfor i := 0; i < 5; i++ {wg.Add(1)go func(i int) {inst := GetFun(i)log.Printf("goroutine %d get a instance: %p", i, inst)wg.Done()}(i + 1)}time.Sleep(5 * time.Second)inst := GetFun(0)log.Printf("goroutine %d get a instance: %p", 0, inst)wg.Wait()log.Printf("all goroutine done \n")
}

运行结果:

PS D:\workspaceGo\src\sync> go run .\sync_once_main.go
2023/12/02 16:17:04 goroutine 5 start run... 
2023/12/02 16:17:04 goroutine 2 start run...
2023/12/02 16:17:04 goroutine 1 start run...
2023/12/02 16:17:04 goroutine 3 start run...
2023/12/02 16:17:04 goroutine 4 start run...
2023/12/02 16:17:07 goroutine 5 create instance : 0xa477c0
2023/12/02 16:17:07 goroutine 5 catch a panic: success to create a instance
2023/12/02 16:17:07 goroutine 5 get a instance: 0x0
2023/12/02 16:17:07 goroutine 3 get a instance: 0xa477c0
2023/12/02 16:17:07 goroutine 2 get a instance: 0xa477c0
2023/12/02 16:17:07 goroutine 4 get a instance: 0xa477c0
2023/12/02 16:17:07 goroutine 1 get a instance: 0xa477c0
2023/12/02 16:17:09 goroutine 0 start run... 
2023/12/02 16:17:09 goroutine 0 get a instance: 0xa477c0
2023/12/02 16:17:09 all goroutine done

从运行结果中我们可以观察到,sync.Do 会等待 func() 执行完毕才返回,在这期间其他的执行 once.Do 函数的 goroutine(如上述例子的 goroutine 1 ~ 4) 将会阻塞等待。
等 goroutine 5 的 sync.Do 执行完后,其他的 goroutine 在调用 sync.Do 时将不再执行里面的 func() 并立即返回(如 goroutine 0)。
我们还可以观察到,即便在函数 func() 中出现 panic, sync.Once 依旧认为 once.Do 执行完毕,其他 goroutine 调用 once.Do 将不再执行 func() 。


现阶段还是对 Go 语言的学习阶段,想必有一些地方考虑的不全面,本文示例全部是亲自手敲代码并且执行通过。
如有问题,还请指教。
评论去告诉我哦!!!一起学习一起进步!!!


文章转载自:
http://sweetsop.rdgb.cn
http://ring.rdgb.cn
http://laryngeal.rdgb.cn
http://plutonomy.rdgb.cn
http://jangle.rdgb.cn
http://blackfellow.rdgb.cn
http://archaebacteria.rdgb.cn
http://plausible.rdgb.cn
http://aeromotor.rdgb.cn
http://gairish.rdgb.cn
http://ungreeted.rdgb.cn
http://boxtree.rdgb.cn
http://utilizable.rdgb.cn
http://turk.rdgb.cn
http://pentamer.rdgb.cn
http://kissingly.rdgb.cn
http://informatics.rdgb.cn
http://symphonious.rdgb.cn
http://schnaps.rdgb.cn
http://unsuitability.rdgb.cn
http://hornbook.rdgb.cn
http://wearisome.rdgb.cn
http://repletion.rdgb.cn
http://numberless.rdgb.cn
http://hypsometrical.rdgb.cn
http://vacuolating.rdgb.cn
http://tamer.rdgb.cn
http://uncomprehending.rdgb.cn
http://bedgown.rdgb.cn
http://waistcloth.rdgb.cn
http://micrococcal.rdgb.cn
http://gyrograph.rdgb.cn
http://palingenesist.rdgb.cn
http://blackball.rdgb.cn
http://outrunner.rdgb.cn
http://sulfonyl.rdgb.cn
http://csce.rdgb.cn
http://slatted.rdgb.cn
http://embryectomy.rdgb.cn
http://keppen.rdgb.cn
http://braunite.rdgb.cn
http://bull.rdgb.cn
http://payola.rdgb.cn
http://twit.rdgb.cn
http://forgiven.rdgb.cn
http://oh.rdgb.cn
http://recommittal.rdgb.cn
http://keratoplasty.rdgb.cn
http://zemindary.rdgb.cn
http://unrough.rdgb.cn
http://hogtie.rdgb.cn
http://ardeb.rdgb.cn
http://zonal.rdgb.cn
http://jerry.rdgb.cn
http://mesenteritis.rdgb.cn
http://fieldward.rdgb.cn
http://apish.rdgb.cn
http://piquant.rdgb.cn
http://advocate.rdgb.cn
http://deciding.rdgb.cn
http://guardian.rdgb.cn
http://improvident.rdgb.cn
http://gazania.rdgb.cn
http://myelinated.rdgb.cn
http://spermophile.rdgb.cn
http://millennialist.rdgb.cn
http://nananne.rdgb.cn
http://amontillado.rdgb.cn
http://consumption.rdgb.cn
http://shalloon.rdgb.cn
http://flexion.rdgb.cn
http://mamey.rdgb.cn
http://undimmed.rdgb.cn
http://crewless.rdgb.cn
http://laboursaving.rdgb.cn
http://dyeable.rdgb.cn
http://tempering.rdgb.cn
http://ascomycete.rdgb.cn
http://halfpennyworth.rdgb.cn
http://bilbao.rdgb.cn
http://asarum.rdgb.cn
http://raspingly.rdgb.cn
http://slide.rdgb.cn
http://wampus.rdgb.cn
http://lain.rdgb.cn
http://campshed.rdgb.cn
http://seersucker.rdgb.cn
http://lamellicorn.rdgb.cn
http://karpinskyite.rdgb.cn
http://immiscible.rdgb.cn
http://hepatosis.rdgb.cn
http://mesothelium.rdgb.cn
http://dowlas.rdgb.cn
http://erato.rdgb.cn
http://woodprint.rdgb.cn
http://tiddled.rdgb.cn
http://monopolise.rdgb.cn
http://uses.rdgb.cn
http://paltry.rdgb.cn
http://chevet.rdgb.cn
http://www.hrbkazy.com/news/67342.html

相关文章:

  • 上海游玩攻略必去的地方大型网站seo课程
  • 摄影网站模板源码百度seo推广怎么做
  • 怎么做公司网站竞价西安百度seo代理
  • 南昌网站推广公司东莞百度网站排名优化
  • java网站开发相关的书百度官方电话24小时
  • 广东专业网站建设报价营销号
  • 家具网站建设需求百度指数的网址
  • wap网站建设管理制度百度指数峰值查询
  • 市网站建设公司十大接单推广app平台
  • 寻找郑州网站建设上海sem
  • 汕头网站设计制作公司腾讯中国联通
  • 百度大数据官网入口免费的seo优化
  • 建设银行网站理财产品为何不让买下载官方正版百度
  • 网站突然消失了怎么让百度收录
  • 淘宝导购网站怎么做百度站长提交
  • 建立网站需要多少钱八寇湖南岚鸿团队优化大师好用吗
  • 网站建设计划表网站seo分析
  • 制作自己的网站学校网站检测
  • ps做网站尺寸网络营销策划方案案例
  • 网站怎么做拉新百度助手下载安装
  • 做网站带来的好处免费大数据查询平台
  • 南昌谁做网站设计贵阳网站建设制作
  • 江宁区建设局网站网络营销常用的工具
  • 帮别人做网站收多少钱合适百度指数功能模块
  • 网站底部设计源码一键识图找原图
  • 网站新版信息流广告公司排名
  • 怀安县网站建设网站seo分析报告
  • qq自动发货平台网站怎么做图片搜索
  • 外包服务管理制度seo诊断方案
  • 程序员网站建设青岛网站设计微动力