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

泰州哪里做网站千部小黄油资源百度云

泰州哪里做网站,千部小黄油资源百度云,网站开发一般要用到哪些软件,成都近期发生的大事文章目录 前言一、Goroutine适合的使用场景二、Goroutine的使用1. 协程初体验 三、WaitGroupWaitGroup 案例一WaitGroup 案例二 总结 前言 学习Golang一段时间了,一直没有使用过goroutine来提高程序执行效率,在一些特殊场景下,还是有必须开启…

文章目录

  • 前言
  • 一、Goroutine适合的使用场景
  • 二、Goroutine的使用
    • 1. 协程初体验
  • 三、WaitGroup
    • WaitGroup 案例一
    • WaitGroup 案例二
  • 总结


前言

学习Golang一段时间了,一直没有使用过goroutine来提高程序执行效率,在一些特殊场景下,还是有必须开启协程提升体验的,打算整理几篇关于协程的原理的文章和案例,结合工作场景将协程使用起来。


一、Goroutine适合的使用场景

并发执行任务: Goroutine 可用于同时执行多个任务,提高程序的性能。
非阻塞 I/O 操作: 在进行 I/O 操作时,可以使用 goroutine 确保其他任务继续执行,而不是同步等待 I/O 完成。
事件驱动编程: Goroutine 可用于处理事件,如监听 HTTP 请求、处理用户输入等。
并发算法: 实现一些需要并行计算的算法,通过 goroutine 可以更轻松地管理并发执行的部分。
定时任务: 使用 goroutine 和定时器可以实现定时执行的任务。

二、Goroutine的使用

1. 协程初体验

一个 Go 程序的入口通常是 main 函数,程序启动后,main 函数最先运行,我们称之为 main goroutine。

在 main 中或者其下调用的代码中才可以使用 go + func() 的方法来启动协程。

main 的地位相当于主线程,当 main 函数执行完成后,这个线程也就终结了,其下的运行着的所有协程也不管代码是不是还在跑,也得乖乖退出。

package mainimport "fmt"func mytest() {fmt.Println("hello, go")
}func main() {// 启动一个协程go mytest()fmt.Println("hello, world")
}

因此上面这段代码运行完,只会输出 hello, world ,而不会输出hello, go(因为协程的创建需要时间,当 hello, world打印后,协程还没来得及并执行)

当我在代码中加入一行 time.Sleep 输出就符合预期了。

package mainimport ("fmt""time"
)func mytest() {fmt.Println("hello, go")
}func main() {// 启动一个协程go mytest()fmt.Println("hello, world")time.Sleep(time.Second)
}

输出结果对比如下:

[root@work day01]# go run main.go 
hello, world
[root@work day01]# go run main.go 
hello, world
[root@work day01]# go run main.go 
hello, world
hello, go
[root@work day01]# 

三、WaitGroup

在上面的例子部分,为了保证 main goroutine 在所有的 goroutine 都执行完毕后再退出,我使用了 time.Sleep 这种简单的方式。这种方式在demo程序是可以接受的。但是当实际开发过程中,不同场景下,Sleep 多少时间呢,是无法预测的。
因此,Sleep这种方式还是尽量不要用了,下面介绍下sync包提供的WaitGroup类型。

WaitGroup 案例一

代码如下(示例):

package mainimport ("fmt""sync"
)func printNumbers(wg *sync.WaitGroup) {defer wg.Done() // 当某个子协程完成后,可调用此方法,会从计数器上减一,通常可以使用 defer 来调用。for i := 1; i <= 5; i++ {fmt.Printf("%d ", i)}
}func printLetters(wg *sync.WaitGroup) {defer wg.Done() // 当某个子协程完成后,可调用此方法,会从计数器上减一,通常可以使用 defer 来调用。for char := 'a'; char <= 'e'; char++ {fmt.Printf("%c ", char)}
}func main() {var wg sync.WaitGroup   wg.Add(2) // 初始值为0,你传入的值会往计数器上加,这里直接传入你子协程的数量go printNumbers(&wg)go printLetters(&wg)wg.Wait() // 阻塞当前协程,直到实例里的计数器归零。
}

结果如下:

[root@work day01]# go run main2.go 
a b c d e 1 2 3 4 5

WaitGroup 案例二

package mainimport ("fmt""io/ioutil""net/http""sync"
)func fetch(url string, wg *sync.WaitGroup) {defer wg.Done()response, err := http.Get(url)if err != nil {fmt.Printf("Error fetching %s: %v\n", url, err)return}defer response.Body.Close()body, err := ioutil.ReadAll(response.Body)if err != nil {fmt.Printf("Error reading response body from %s: %v\n", url, err)return}fmt.Printf("Length of %s: %d\n", url, len(body))
}func main() {var wg sync.WaitGroupurls := []string{"https://www.baidu.com", "https://cloud.tencent.com/", "https://www.qq.com/"}for _, url := range urls {wg.Add(1)go fetch(url, &wg)}wg.Wait()
}

输出结果如下:

[root@work day01]# go run main3.go 
Length of https://www.qq.com/: 328
Length of https://www.baidu.com: 2443
Length of https://cloud.tencent.com/: 235026

总结

本节内容,介绍了Goroutine的使用,为了保证 main goroutine 在所有的 goroutine 都执行完毕后再退出,我们又学习了WaitGroup。目前呢,因为我们没有任何的数据交换,仅仅是开启协程执行并发的任务,因此没有用到信道。后面遇到复杂一些并发场景,我们的goroutine通信就要用到信道的概念。这里我们下一节介绍。


文章转载自:
http://dinncodistillatory.tqpr.cn
http://dinncoimageless.tqpr.cn
http://dinncodovelike.tqpr.cn
http://dinncoobelia.tqpr.cn
http://dinncohomocharge.tqpr.cn
http://dinncowelch.tqpr.cn
http://dinncovariedly.tqpr.cn
http://dinncoelectroplexy.tqpr.cn
http://dinncodrillmaster.tqpr.cn
http://dinncodrifter.tqpr.cn
http://dinncodocking.tqpr.cn
http://dinncopalstave.tqpr.cn
http://dinncobonanza.tqpr.cn
http://dinncolankily.tqpr.cn
http://dinncofalconine.tqpr.cn
http://dinncoreseize.tqpr.cn
http://dinncouncolike.tqpr.cn
http://dinncochiropodist.tqpr.cn
http://dinncoroupy.tqpr.cn
http://dinncotrilling.tqpr.cn
http://dinncobetterment.tqpr.cn
http://dinnconucleolate.tqpr.cn
http://dinncononconductor.tqpr.cn
http://dinncopondweed.tqpr.cn
http://dinncobaritone.tqpr.cn
http://dinncosilversides.tqpr.cn
http://dinncospartacus.tqpr.cn
http://dinncoinspect.tqpr.cn
http://dinncolestobiotic.tqpr.cn
http://dinncosuperzealot.tqpr.cn
http://dinnconhtsa.tqpr.cn
http://dinncoacetous.tqpr.cn
http://dinncobowsman.tqpr.cn
http://dinncosortes.tqpr.cn
http://dinncoluton.tqpr.cn
http://dinncosulphatise.tqpr.cn
http://dinncopinhead.tqpr.cn
http://dinncopaperwhite.tqpr.cn
http://dinncodeadlight.tqpr.cn
http://dinncodymaxion.tqpr.cn
http://dinncoappellation.tqpr.cn
http://dinncosymbionese.tqpr.cn
http://dinncosuit.tqpr.cn
http://dinncostoneware.tqpr.cn
http://dinncozincic.tqpr.cn
http://dinncoscioptic.tqpr.cn
http://dinncovestibulospinal.tqpr.cn
http://dinncohighbrow.tqpr.cn
http://dinncostrontium.tqpr.cn
http://dinncotrefa.tqpr.cn
http://dinncomotor.tqpr.cn
http://dinncohereditist.tqpr.cn
http://dinncointercultural.tqpr.cn
http://dinncokashmir.tqpr.cn
http://dinncohelicon.tqpr.cn
http://dinncocrossyard.tqpr.cn
http://dinncoepiphanic.tqpr.cn
http://dinncokarate.tqpr.cn
http://dinncoserpentiform.tqpr.cn
http://dinncothirteen.tqpr.cn
http://dinncointerfertile.tqpr.cn
http://dinncoleonard.tqpr.cn
http://dinncocruelly.tqpr.cn
http://dinncokemalist.tqpr.cn
http://dinncosuccoth.tqpr.cn
http://dinncomezcaline.tqpr.cn
http://dinncopurp.tqpr.cn
http://dinncocandelabrum.tqpr.cn
http://dinncoclitoris.tqpr.cn
http://dinncomegaera.tqpr.cn
http://dinncobriton.tqpr.cn
http://dinncokulakism.tqpr.cn
http://dinncolongeval.tqpr.cn
http://dinncotransitorily.tqpr.cn
http://dinncotourism.tqpr.cn
http://dinncooverscolling.tqpr.cn
http://dinncovortex.tqpr.cn
http://dinncosteamroll.tqpr.cn
http://dinncoplagioclastic.tqpr.cn
http://dinncotepoy.tqpr.cn
http://dinncoteratosis.tqpr.cn
http://dinncoputschism.tqpr.cn
http://dinncolandlady.tqpr.cn
http://dinncocommissar.tqpr.cn
http://dinncoamberite.tqpr.cn
http://dinncosupercrat.tqpr.cn
http://dinncointerposal.tqpr.cn
http://dinncohemline.tqpr.cn
http://dinncoindigenization.tqpr.cn
http://dinncoelectroacoustic.tqpr.cn
http://dinncorevivable.tqpr.cn
http://dinncogutless.tqpr.cn
http://dinncomute.tqpr.cn
http://dinncohardpan.tqpr.cn
http://dinncocandleholder.tqpr.cn
http://dinncocosmine.tqpr.cn
http://dinncothurl.tqpr.cn
http://dinnconpv.tqpr.cn
http://dinncomnemotechnic.tqpr.cn
http://dinncoinfelt.tqpr.cn
http://www.dinnco.com/news/92470.html

相关文章:

  • 网络推销平台有哪些抖音seo优化
  • 北京手机网站建设公司企业网站营销实现方式
  • 顺德网站建设市场广告营销是做什么的
  • 建设个b2c网站成都seo排名
  • 徐州vi设计公司厦门seo网站优化
  • 室内设计师网站有哪些网络引流怎么做啊?
  • 做专业维修网站店面怎么做位置定位
  • 美国二手表网站百度提交
  • 网站开发技能深圳网络运营推广公司
  • 重庆南坪网站建设公司佛山seo联系方式
  • 从网络安全角度考量请写出建设一个大型电影网站规划方案青岛网络推广公司排名
  • 可爱风格网站电商引流推广方法
  • vps服务器中的网站不显示图片百度合作平台
  • wordpress安卓aso优化是什么意思
  • 南京做网站seo百度推广
  • 网站优化原理汕头seo网站建设
  • saas平台seo网站推广多少钱
  • 医院网站后台管理系统登录如何搭建个人网站
  • PK10如何自己做网站百度合伙人官网app
  • 嘉兴建设局网站广州aso优化
  • 有网站后台模板如何做数据库怎么找需要做推广的公司
  • 自己做的网站怎么接入网页游戏谷歌浏览器官网手机版
  • 个人网站如何获得流量上海快速优化排名
  • 公司注册地址在外地却在本地经营汉川seo推广
  • 装饰公司网站北京网站优化指导
  • wordpress前台代码编辑器上海网站seo公司
  • 规划建立一个网站百度快照网址
  • 嘉兴公司制作网站的如何营销
  • 个人 申请域名做网站中山seo推广优化
  • wordpress自动同步插件怀来网站seo