golang提供内建函数cap用于查看channel缓冲区长度。

cap的定义如下:

func cap(v Type) int
The cap built-in function returns the capacity of v, according to its type:
- Array: the number of elements in v (same as len(v)).等同于len - Pointer to array: the number of elements in *v (same as len(v)).等同于len - Slice: the maximum length the slice can reach when resliced;
if v is nil, cap(v) is zero.对于slice,表示在不重新分配空间的情况下,可以达到的切片的最大长度。如果切片是nil, 则长度为0. - Channel: the channel buffer capacity, in units of elements;表示缓冲区的长度。
if v is nil, cap(v) is zero. 如果通道是nil,则缓冲区长度为0。

Example

package main

import ("fmt")

func main(){

	ch1 := make(chan int)
ch2 := make(chan int, 2)//缓冲区长度为2 fmt.Println("ch1 buffer len:", cap(ch1))
fmt.Println("ch2 buffer len:", cap(ch2))
}

output:

ch1 buffer len:0

ch2 buffer len:2

golang查看channel缓冲区的长度的更多相关文章

  1. golang 如何查看channel通道中未读数据的长度

    可以通过内建函数len查看channel中元素的个数. 内建函数len的定义如下: func len(v Type) int The len built-in function returns the ...

  2. golang的Channel

    golang的Channel Channel 是 golang 一个非常重要的概念,如果你是刚开始使用 golang 的开发者,你可能还没有真正接触这一概念,本篇我们将分析 golang 的Chann ...

  3. go channel缓冲区的大小

    go channel缓冲区的大小 len也可以作用于channel,代表现在channel缓冲区中还有多少数据没有读取.示例如下 c:=make(chan int,20) fmt.Println(&q ...

  4. c#实现golang 的channel

    使用.NET的 BlockingCollection<T>来包装一个ConcurrentQueue<T>来实现golang的channel. 代码如下: public clas ...

  5. golang的channel实现

    golang的channel实现位于src/runtime/chan.go文件.golang中的channel对应的结构是: // Invariants: // At least one of c.s ...

  6. Golang 入门 : channel(通道)

    笔者在<Golang 入门 : 竞争条件>一文中介绍了 Golang 并发编程中需要面对的竞争条件.本文我们就介绍如何使用 Golang 提供的 channel(通道) 消除竞争条件. C ...

  7. 深入学习golang(2)—channel

    Channel 1. 概述 “网络,并发”是Go语言的两大feature.Go语言号称“互联网的C语言”,与使用传统的C语言相比,写一个Server所使用的代码更少,也更简单.写一个Server除了网 ...

  8. Golang | 简介channel常见用法,完成goroutin通信

    今天是golang专题的第14篇文章,大家可以点击上方的专辑回顾之前的内容. 今天我们来看看golang当中另一个很重要的概念--信道.我们之前介绍goroutine的时候曾经提过一个问题,当我们启动 ...

  9. 【GoLang】golang context channel 详解

    代码示例: package main import ( "fmt" "time" "golang.org/x/net/context" ) ...

随机推荐

  1. linux和 unix 介绍

    linux和unix都是当今鼎鼎大名的操作系统,可以说改变了这个世界,也是当今科技产业的重要基础.让我们回顾一下他们的发展史吧. 1.unix起源. 上世纪六十年代时,大部份计算机都是采用批处理的方式 ...

  2. :after和:before伪元素的解释

    :after  是清除浮动  让其高度和内容高度相同 :before   是防止上边塌陷 关注微信小程序

  3. Spring Boot 揭秘与实战(五) 服务器篇 - Tomcat 启用 HTTPS

    文章目录 1. 生成证书 2. 配置 HTTPS 支持 3. 启动与测试 4. 源代码 Spring Boot 内嵌的 Tomcat 服务器可以启用 HTTPS 支持. 生成证书 使用第三方 CA 证 ...

  4. SpringBatch Sample (四)(固定长格式文件读写)

    前篇关于Spring Batch的文章,讲述了Spring Batch 对XML文件的读写操作. 本文将通过一个完整的实例,与大家一起讨论运用Spring Batch对固定长格式文件的读写操作.实例延 ...

  5. [opencvjichu]cv::Mat::type() 返回值

    opencv opencv中Mat存在各种类型,其中mat有一个type()的函数可以返回该Mat的类型.类型表示了矩阵中元素的类型以及矩阵的通道个数,它是一系列的预定义的常量,其命名规则为CV_(位 ...

  6. hdoj-1022(栈的模拟)

    #include <iostream> #include <cstring> #include <algorithm> #include <stack> ...

  7. Hierarchical RNN

    https://blog.csdn.net/liuchonge/article/details/73610734 https://blog.csdn.net/triplemeng/article/de ...

  8. 自定义bootbox:dialog方法

    <script src="static/ace/js/bootbox.js"></script>bootbox.dialog({ message: '< ...

  9. 无用之学matplotlib,numpy,pandas

    一.matplotlib学习 matplotlib: 最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建 例子1: # coding=utf- from ...

  10. 识别假tf卡工具

    h2testwhttps://www.heise.de/download/product/h2testw-50539使用HaraldBögeholz的免费测试工具H2testw,可以检查存储介质(如硬 ...