zoukankan      html  css  js  c++  java
  • go语言从例子开始之Example23.通道缓冲

    默认通道是 无缓冲 的,这意味着只有在对应的接收(<- chan)通道准备好接收时,才允许进行发送(chan <-)。可缓存通道允许在没有对应接收方的情况下,缓存限定数量的值。

    不支持缓冲:

    mk := make(chan string)
    通道不支持缓存,如果进行缓冲报如下错误。
    mk <- "chan"
    fatal error: all goroutines are asleep - deadlock!

    Example:

    package main
    
    import "fmt"
    
    
    
    func main(){
        //使用 make(chan val-type) 创建一个新的通道。通道类型就是他们需要传递值的类型,最多缓存两个值
        mk := make(chan string, 2)
    
        mk <- "buf"
        mk <- "chan"
        fmt.Println(<- mk)
        fmt.Println(<- mk)
    }

    Result:

    $ go run example.go
    buf
    chan

    坐标: 上一个例子    下一个例子

  • 相关阅读:
    集合框架
    hashtable
    测试3
    opcache的威力
    信息的信息
    php blog to explore
    BEHAT安装
    Failed to start: SocketListener0@0.0.0.0:4444
    模板方法设计模式
    mysqldump
  • 原文地址:https://www.cnblogs.com/yhleng/p/11750875.html
Copyright © 2011-2022 走看看