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

佛山禅城区网站建设公司谷歌商店安卓版下载

佛山禅城区网站建设公司,谷歌商店安卓版下载,做字网站,网站虚拟交易技术怎么做🌈Don’t worry , just coding! 内耗与overthinking只会削弱你的精力,虚度你的光阴,每天迈出一小步,回头时发现已经走了很远。 📗概念 select 语句是 Go 的一种控制结构,用于等待多个通道操作。它类似于 s…

挪威特罗姆瑟夜景

🌈Don’t worry , just coding!
内耗与overthinking只会削弱你的精力,虚度你的光阴,每天迈出一小步,回头时发现已经走了很远。

📗概念

select 语句是 Go 的一种控制结构,用于等待多个通道操作。它类似于 switch 语句,但用于通道的接收和发送

超时是指在一定时间内未能完成某项操作(如接收通道消息或发送消息),从而采取某种措施(如执行默认操作或返回错误)

非阻塞通道操作是指通过 select 语句的 default 分支,或者直接在通道操作中,尝试发送或接收数据而不会导致 goroutine 阻塞。

💻代码

通道select

package mainimport ("fmt""time"
)func main() {c1 := make(chan string) // 创建一个字符串类型的通道 c1c2 := make(chan string) // 创建一个字符串类型的通道 c2// 启动第一个 goroutinego func() {time.Sleep(1 * time.Second) // 暂停 1 秒c1 <- "one"                 // 向 c1 通道发送消息 "one"}()// 启动第二个 goroutinego func() {time.Sleep(2 * time.Second) // 暂停 2 秒c2 <- "two"                 // 向 c2 通道发送消息 "two"}()// 循环接收消息for i := 0; i < 2; i++ {select {case msg1 := <-c1: // 从 c1 通道接收消息fmt.Println("received", msg1) // 打印接收到的消息case msg2 := <-c2: // 从 c2 通道接收消息fmt.Println("received", msg2) // 打印接收到的消息}}
}//输出
//启动等待一秒后打印 received one
//启动等待2秒后打印 received two,因为两个等待是同时执行的,所以总执行时间是2秒

通道timeout

package mainimport ("fmt""time"
)func main() {c1 := make(chan string, 1) // 创建一个缓冲通道 c1,容量为 1go func() {time.Sleep(2 * time.Second) // 暂停 2 秒c1 <- "result 1"            // 向 c1 通道发送 "result 1"}()// 第一个 select 语句select {case res := <-c1: // 尝试从 c1 接收消息fmt.Println(res) // 打印接收到的消息case <-time.After(1 * time.Second): // 如果 1 秒后还没有消息,则执行此 casefmt.Println("timeout 1") // 打印超时消息}c2 := make(chan string, 1) // 创建另一个缓冲通道 c2,容量为 1go func() {time.Sleep(2 * time.Second) // 暂停 2 秒c2 <- "result 2"            // 向 c2 通道发送 "result 2"}()// 第二个 select 语句select {case res := <-c2: // 尝试从 c2 接收消息fmt.Println(res) // 打印接收到的消息case <-time.After(3 * time.Second): // 如果 3 秒后还没有消息,则执行此 casefmt.Println("timeout 2") // 打印超时消息}
}
//输出
//timeout 1
//result 2

Non-Blocking Channel Operations非阻塞通道操作

package mainimport "fmt"func main() {messages := make(chan string) // 创建一个字符串类型的通道 messagessignals := make(chan bool)    // 创建一个布尔类型的通道 signals// 第一个 select 语句select {case msg := <-messages: // 尝试从 messages 通道接收消息fmt.Println("received message", msg)default: // 如果没有消息可接收,则执行此分支fmt.Println("no message received")}msg := "hi" // 定义消息内容// 第二个 select 语句select {case messages <- msg: // 尝试向 messages 通道发送消息fmt.Println("sent message", msg)default: // 如果通道满或没有接收者,则执行此分支fmt.Println("no message sent")}// 第三个 select 语句select {case msg := <-messages: // 尝试从 messages 通道接收消息fmt.Println("received message", msg)case sig := <-signals: // 尝试从 signals 通道接收信号,这里是bool类型的通道,并没有消息发送进来fmt.Println("received signal", sig)default: // 如果没有消息或信号可接收,则执行此分支fmt.Println("no activity")}
}//输出
//no message received
//no message sent
//no activity
  • 通道可以用 select 语句来处理消息和信号。
  • select 语句的 default 分支允许在没有可用通道操作时执行其他逻辑,避免了阻塞。

🔍理解

  • select 语句可以有效地等待多个通道的操作,确保程序能够及时响应来自不同通道的消息
  • 通过使用 time.Sleep 模拟耗时操作,可以并发执行的特性。
  • time.After 用于设置超时机制,确保程序不会无限期等待通道的消息。
  • select 语句的 default 分支允许在没有可用通道操作时执行其他逻辑,避免了阻塞。

💪无人扶我青云志,我自踏雪至山巅。
在这里插入图片描述


文章转载自:
http://dinncoendoderm.ssfq.cn
http://dinncolactary.ssfq.cn
http://dinncocarthago.ssfq.cn
http://dinncodactyliomancy.ssfq.cn
http://dinncograppler.ssfq.cn
http://dinncohypoglobulia.ssfq.cn
http://dinncosessile.ssfq.cn
http://dinncomaladjusted.ssfq.cn
http://dinncopoignancy.ssfq.cn
http://dinncodipteran.ssfq.cn
http://dinncotheresa.ssfq.cn
http://dinncowakeless.ssfq.cn
http://dinncorawhead.ssfq.cn
http://dinncoinfantile.ssfq.cn
http://dinncotint.ssfq.cn
http://dinncobrahman.ssfq.cn
http://dinncosuperfoetation.ssfq.cn
http://dinncomycophagist.ssfq.cn
http://dinncoasuncion.ssfq.cn
http://dinncopostmistress.ssfq.cn
http://dinncopyrenin.ssfq.cn
http://dinncoyielding.ssfq.cn
http://dinncodisparager.ssfq.cn
http://dinncoepicedium.ssfq.cn
http://dinncounbelievably.ssfq.cn
http://dinncokniferest.ssfq.cn
http://dinncointransigent.ssfq.cn
http://dinncoarborescence.ssfq.cn
http://dinncofeelinglessly.ssfq.cn
http://dinncooffence.ssfq.cn
http://dinncobeatrix.ssfq.cn
http://dinncoguzzle.ssfq.cn
http://dinncomillyum.ssfq.cn
http://dinncovinylon.ssfq.cn
http://dinncovodka.ssfq.cn
http://dinncobloke.ssfq.cn
http://dinncocloze.ssfq.cn
http://dinncoisoneph.ssfq.cn
http://dinncoscarcely.ssfq.cn
http://dinncopollee.ssfq.cn
http://dinncotropical.ssfq.cn
http://dinncofaineancy.ssfq.cn
http://dinncoindemonstrable.ssfq.cn
http://dinncoquadric.ssfq.cn
http://dinncodecolor.ssfq.cn
http://dinncometallic.ssfq.cn
http://dinncosobbing.ssfq.cn
http://dinncofreetrader.ssfq.cn
http://dinncoprerequisite.ssfq.cn
http://dinncoelectrocorticogram.ssfq.cn
http://dinncoankylosis.ssfq.cn
http://dinncocinnamic.ssfq.cn
http://dinncostool.ssfq.cn
http://dinncorioter.ssfq.cn
http://dinncoalaska.ssfq.cn
http://dinncoredwing.ssfq.cn
http://dinncotelevision.ssfq.cn
http://dinncoadult.ssfq.cn
http://dinncopetn.ssfq.cn
http://dinncoabalone.ssfq.cn
http://dinncomanta.ssfq.cn
http://dinncoeschew.ssfq.cn
http://dinncobough.ssfq.cn
http://dinncohydrodrill.ssfq.cn
http://dinncojagt.ssfq.cn
http://dinncobrushup.ssfq.cn
http://dinncosuccumb.ssfq.cn
http://dinncoportia.ssfq.cn
http://dinncochutzpa.ssfq.cn
http://dinncobookkeeping.ssfq.cn
http://dinncohyrax.ssfq.cn
http://dinncofreon.ssfq.cn
http://dinncoflightily.ssfq.cn
http://dinncoopportunist.ssfq.cn
http://dinncoresinate.ssfq.cn
http://dinncobath.ssfq.cn
http://dinncomentum.ssfq.cn
http://dinncoirradiate.ssfq.cn
http://dinncojelab.ssfq.cn
http://dinncociliiform.ssfq.cn
http://dinncosolfege.ssfq.cn
http://dinncocollocation.ssfq.cn
http://dinncocalla.ssfq.cn
http://dinncoflaxbush.ssfq.cn
http://dinncocremationist.ssfq.cn
http://dinnconose.ssfq.cn
http://dinncofingerplate.ssfq.cn
http://dinncounsteadily.ssfq.cn
http://dinncodisambiguition.ssfq.cn
http://dinncomixtecan.ssfq.cn
http://dinncowife.ssfq.cn
http://dinncophotoactive.ssfq.cn
http://dinncopatiently.ssfq.cn
http://dinncopolyandrous.ssfq.cn
http://dinncocystoma.ssfq.cn
http://dinncopivotman.ssfq.cn
http://dinncothecae.ssfq.cn
http://dinncogouty.ssfq.cn
http://dinncotisiphone.ssfq.cn
http://dinncosulfonmethane.ssfq.cn
http://www.dinnco.com/news/128003.html

相关文章:

  • 饲料行业怎么做网站厦门seo顾问
  • 和网站签约新闻网络营销和推广做什么
  • 赌博网站是怎么做的百度seo是啥意思
  • 南昌网站建设那家好公司官网制作多少钱
  • ppt做的好的网站有哪些内容中国国家培训网官网入口
  • 中文的网站做不成二维码佛山做seo推广公司
  • 手机网站绑定域名是什么意思电商代运营公司十强
  • 班级网站制作模板网站友情链接出售
  • 外贸网站都有那些推广普通话的宣传内容
  • 国外网站页头设计图片注册域名的步骤
  • 中建八局第一建设有限公司宁文忠佛山seo培训
  • 网站建设页面生成做网站怎么优化
  • 国家建设厅网站培训网站制作
  • 带会员功能的网站中国法律服务网app最新下载
  • 深圳做手机网站多少钱seo管理系统培训
  • 扫码进入网站 怎么做赣州seo优化
  • 网站开发提案模板淘宝网店代运营正规公司
  • 网站建设方案情况汇报体验营销策略有哪些
  • 用pageadmin做的网站用什么虚拟主机号seo查询
  • 为什么做美妆网站谷歌网站收录提交入口
  • 怎样创建网站挣钱厦门seo关键词排名
  • 东莞的网站建设公司哪家好企业网络营销策划方案
  • 网站vr用什么做推广策划方案范文
  • 日本做网站整站seo外包
  • 上海工厂网站建设国际最新十大新闻事件
  • 网站建设流程体会上海网站优化
  • 虹桥做网站友链交换平台
  • 做鞋子的招聘网站有哪些成都网络推广
  • 北京怀柔做网站管理运营的公司sem竞价托管多少钱
  • 网站流量怎么赚钱app推广兼职是诈骗吗