美文网首页
Golang learning 通道 Chanel

Golang learning 通道 Chanel

作者: wangyongyue | 来源:发表于2019-05-21 17:35 被阅读0次

通道Chanel 可以连接两个goroutine ,可以让一个goroutine发送特定的值到另外一个goroutine。

c := make(chan int)  实例int channel 
go func() {
     c <- 100              给channel赋值100
}()

a := <- c                  接收channel赋值100
fmt.Println(a)           打印

channe 有两种形式无缓冲和有缓冲
无缓冲的,一个线程向channel 发送了消息,直到有其他线程接收消息,否则会一直阻塞当前线程

c := make(chan int)
go func() {
     c <- 100             
}()

a := <- c                 
fmt.Println(a)         

有缓冲的,可以指定消息数量,如果消息数量大于指定数量,必须有其他线程接收消息,否则阻塞
c := make(chan int,1)
go func() {
fmt.Println("main1")
c <- 100

}()

go func() {
    fmt.Println("main2")
    c <- 200


}()

a := <- c
fmt.Println(a)

相关文章

  • Golang learning 通道 Chanel

    通道Chanel 可以连接两个goroutine ,可以让一个goroutine发送特定的值到另外一个gorout...

  • Golang的通道简介以及通道的作用

    chanel通道,在golang的变量为chan,为什么要单独开一章出来讲解呢,主要是chan是一个挺重要的东西,...

  • Introduction

    This is the notes written for the learning of GoLang Stru...

  • golang chanel疑惑

    今天看golang chan发现了一个奇怪的问题,就是1.同样的不带缓存chanel,已有数据关闭和没有数据关闭,...

  • 通道--golang

    通道(channels) 是连接多个协程的管道,可以从一个协程将值发送到通道,然后在另一个协程中接收,由关键字ch...

  • 动效篇--AE特效中英文对照查询表

    3D Channel三维通道特效 --3d chanel extract 提取三维通道--depth matte深...

  • AE特效中英文對照

    3D Channel三维通道特效 --3d chanel extract 提取三维通道 --depth matte...

  • AE内置插件中英文对照

    3D Channel三维通道特效 name名字3d chanel extract提取三维通道depth matte...

  • Go 语言入门

    原文https://milapneupane.com.np/2019/07/06/learning-golang-...

  • GO语言入门教程

    本文翻译于Milap Neupane Blog的Learning Golang — from zero to he...

网友评论

      本文标题:Golang learning 通道 Chanel

      本文链接:https://www.haomeiwen.com/subject/nwgjzqtx.html