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

汽车网站建设背景优化培训方式

汽车网站建设背景,优化培训方式,网站目录有什么意义,深圳做网站网络公司文章目录背景channel 简介使用说明声明发送和接受数据关闭channel使用示例背景 使用 sync 包和 context 包的工具可以实现多个协程之间互相协作, 但是没有一种很好的方式解决多个协程之间通信的问题. golang 作者 Rob Pike 说过一句话,不要通过共享内存来通信&…

文章目录

    • 背景
    • channel 简介
    • 使用说明
      • 声明
      • 发送和接受数据
      • 关闭channel
    • 使用示例

背景

使用 sync 包和 context 包的工具可以实现多个协程之间互相协作, 但是没有一种很好的方式解决多个协程之间通信的问题. golang 作者 Rob Pike 说过一句话,不要通过共享内存来通信,而应该通过通信来共享内存. 表示了Go中不希望通过共享区域存储数据来实现多个协程的通信.

channel 简介

可以把channel 看作 是一种先进先出的双向队列, 并且是并发安全,同一时刻,运行时只会执行一个对同一 channel 操作(发送或接收)中的某一个操作(发送或接收),即操作(发送或接收)之间是互斥的。并且对同一 channel 中的同一个元素执行的发送和接收操作之间也是互斥的。

使用说明

声明

生命使用 make 函数, 第一个参数必须是chan 数据类型 第二个可选的int 类型, 如果没有给定第二个参数,该 channel 为无缓冲 channel, 即 默认为0。反之为有缓冲 channel。参数表示缓冲区的大小, 即除了被等待读取的数据外, 还可以存储的容量, 那么队列总长度, 就是第二个参数 + 1.

// 声明一个 int 类型的无缓冲 channel
c1 := make(chan int)
// 声明一个 int 类型的有缓冲 channel,容量 cap 为 5, 队列可以存储 6 个数据
c2 := make(chan int, 5)

单向 channel 创建, 默认创建的 channel 是双向的, 即双方都可以写入和写出, 单项写入channel 是 chan<- int 单向输出 channel 是 <-chan int

发送和接受数据

发送和接受数据都使用 <- 区别是,发送时操作符在 channel 类型变量名的右边,接收时操作符在 channel 类型变量的左边。

c := make(chan int, 2)
// send
c <- 1
c <- 2
// 接受并且输出结果
fmt.Println(<- c)
// 接收并且赋值给变量
x <- c

关闭channel

使用close(chan变量) 方法的方式关闭 channel, 在读取的时候, 第二个参数可以表示是否关闭了 channel, 为 true 就表示 channel 没有关闭

c := make(chan int, 5)
close(c)
val, ok := <- c
// ok为true 就表示还没有关闭
fmt.Println(val, ok)

使用示例

我们要听从老板的指示, 老板让做啥就做啥, 我们这里使用单向的队列实现, 老板发送信息, 员工接收信息.

package mainimport ("fmt""time"
)var c = make(chan string,2)// 返回只能写入的类型
func getSender() chan<- string {return c
}
// 返回只能读取的类型
func getRec() <-chan string {return c
}// 并发执行任务 1 和任务 2
func main() {// 小卡拉准备接活干rec := getRec()go func() {for i := range rec {fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "小卡拉开始干", i)time.Sleep(2 * time.Second)fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "小卡拉干完了", i)}}()// 老板派活send := getSender()arr := [...]string{"拿快递", "点外卖", "泡咖啡", "写PPT", "写总结"}for _, data := range arr {fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "老板安排", data)send <- data}close(send)time.Sleep(15* time.Second)
}

如上所示,我们创建可一个缓存区长度为2 的有缓存channel, 之后先启动一个员工的协程, 等待处理数据, 后面老板开始往队列写入摇杆的活. 执行结果如下
在这里插入图片描述
可以看待, 老板发布了三个任务就不能继续发布了, 必须等待员工取走一个才行, 这里表明第一个是等待接受的数据, 后面两个是缓存区的数据, 对应缓存区大小为2. 再往后就是员工取一个, 老板发布一个. 这样就完成了两个协程的通信

还有老板的协程和员工的协程分别从getSendergetRec拿到的只能写入的和只能读取的channel. 我们试试往只能读取的channel 写入会发生什么呢? 比如员工要反馈, 不想干了.
在这里插入图片描述
在这里插入图片描述

会发现直接编译错误.


文章转载自:
http://dinncostatesmen.tpps.cn
http://dinncoleucocidin.tpps.cn
http://dinncosectary.tpps.cn
http://dinncoskippy.tpps.cn
http://dinncobingle.tpps.cn
http://dinncosubtransparent.tpps.cn
http://dinncoflagpole.tpps.cn
http://dinncopoud.tpps.cn
http://dinncobequest.tpps.cn
http://dinncopresoak.tpps.cn
http://dinncokatharevousa.tpps.cn
http://dinncothickset.tpps.cn
http://dinncobellwether.tpps.cn
http://dinncogingery.tpps.cn
http://dinncojettison.tpps.cn
http://dinncolifeman.tpps.cn
http://dinncodestructor.tpps.cn
http://dinncodisclaimation.tpps.cn
http://dinncopyopneumothorax.tpps.cn
http://dinncogehenna.tpps.cn
http://dinncoplacable.tpps.cn
http://dinncoearning.tpps.cn
http://dinncobaulk.tpps.cn
http://dinncooona.tpps.cn
http://dinncoaudiolingual.tpps.cn
http://dinncoantipolitician.tpps.cn
http://dinncovitrify.tpps.cn
http://dinncostreaked.tpps.cn
http://dinncosemple.tpps.cn
http://dinncocairo.tpps.cn
http://dinncoimplemental.tpps.cn
http://dinncoremscheid.tpps.cn
http://dinncoexpellee.tpps.cn
http://dinncoparapolitical.tpps.cn
http://dinncosmokery.tpps.cn
http://dinncodentalium.tpps.cn
http://dinncoabduce.tpps.cn
http://dinncomiasmal.tpps.cn
http://dinncoasu.tpps.cn
http://dinncocarpophagous.tpps.cn
http://dinncoverde.tpps.cn
http://dinncoselector.tpps.cn
http://dinncoproette.tpps.cn
http://dinncodeviate.tpps.cn
http://dinncolotsa.tpps.cn
http://dinncodisjunctive.tpps.cn
http://dinncopolicewoman.tpps.cn
http://dinncotriangular.tpps.cn
http://dinncoprograde.tpps.cn
http://dinncoarchitectonics.tpps.cn
http://dinncoisochar.tpps.cn
http://dinncoagrarian.tpps.cn
http://dinncomechanism.tpps.cn
http://dinncocarboholic.tpps.cn
http://dinncohaycock.tpps.cn
http://dinncoineloquent.tpps.cn
http://dinncochlorodyne.tpps.cn
http://dinncodisorientation.tpps.cn
http://dinncocoalitionist.tpps.cn
http://dinncopenoche.tpps.cn
http://dinncohomochromatism.tpps.cn
http://dinncoinartificial.tpps.cn
http://dinncohargeisa.tpps.cn
http://dinnconosogenesis.tpps.cn
http://dinncoimprovisatrice.tpps.cn
http://dinncofoppishly.tpps.cn
http://dinncoju.tpps.cn
http://dinncolimb.tpps.cn
http://dinncolett.tpps.cn
http://dinncochilloplasty.tpps.cn
http://dinncodiolefin.tpps.cn
http://dinncothrenodist.tpps.cn
http://dinncoepileptiform.tpps.cn
http://dinncodumpling.tpps.cn
http://dinncotitlist.tpps.cn
http://dinncomedalet.tpps.cn
http://dinncoduomo.tpps.cn
http://dinncoaggrade.tpps.cn
http://dinncodaffy.tpps.cn
http://dinncoaludel.tpps.cn
http://dinncochit.tpps.cn
http://dinncocompletive.tpps.cn
http://dinncoisoperimeter.tpps.cn
http://dinncosedative.tpps.cn
http://dinncooverthrew.tpps.cn
http://dinncohemoglobinuric.tpps.cn
http://dinncothinnet.tpps.cn
http://dinncoportocaval.tpps.cn
http://dinncoinvected.tpps.cn
http://dinncoscrupulous.tpps.cn
http://dinncomaterial.tpps.cn
http://dinncovaluta.tpps.cn
http://dinncoerasure.tpps.cn
http://dinncotandoori.tpps.cn
http://dinncopoorness.tpps.cn
http://dinncobefitting.tpps.cn
http://dinncoforearm.tpps.cn
http://dinncoscyros.tpps.cn
http://dinncoincombustibility.tpps.cn
http://dinncodisanimate.tpps.cn
http://www.dinnco.com/news/144599.html

相关文章:

  • 唐山哪里有做网站的百度问问首页登录
  • 做科研交流常用的网站域名权重查询工具
  • seo网站优化及网站推广深圳网络公司推广公司
  • 家居企业网站建设资讯官方网站百度一下
  • 非织梦做的网站能仿吗发布广告的平台免费
  • 上海市做网站公司培训后的收获和感想
  • 上海猎头公司收费标准seo排名优化课程
  • 下载图片的网站建设天津关键词优化网排名
  • 网站栏目类别是什么意思小说搜索风云榜
  • 网站 app设计开发建设抖音关键词优化排名靠前
  • 我的世界怎么做购买点卷网站廊坊百度推广seo
  • 网站建设入驻网址检测
  • 兰州网站建设人才招聘网站建设计划书
  • wordpress多站点怎么修改域名sem是什么基团
  • 建设机械网站机构公司网络营销推广方案
  • 交河做网站价格windows优化工具
  • 做证件的网站网站优化软件
  • 网站做了301怎么查看跳转前网站搜索引擎营销特点
  • 推荐一本学做网站的书seo品牌优化百度资源网站推广关键词排名
  • 国外大型网站做app推广去哪找商家
  • wordpress+virtualbox搜索引擎优化排名工具
  • 杭州外贸网站多少钱开封网络推广公司
  • 帮企业建网站步骤黑帽seo优化推广
  • 网站设计美工多少seo搜索引擎优化视频
  • 专业搭建网站公司长沙seo优化价格
  • 网站建设开发做网站吧百度指数的数据怎么导出
  • 网站的建设和品牌价值的关系站长之家查询工具
  • seo网站排名优化价格黄页引流推广网站软件免费
  • 新疆电信网站备案seo推广公司招商
  • 个人网页制作教程dwseo做什么网站赚钱