golang查看channel缓冲区的长度
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缓冲区的长度的更多相关文章
- golang 如何查看channel通道中未读数据的长度
可以通过内建函数len查看channel中元素的个数. 内建函数len的定义如下: func len(v Type) int The len built-in function returns the ...
- golang的Channel
golang的Channel Channel 是 golang 一个非常重要的概念,如果你是刚开始使用 golang 的开发者,你可能还没有真正接触这一概念,本篇我们将分析 golang 的Chann ...
- go channel缓冲区的大小
go channel缓冲区的大小 len也可以作用于channel,代表现在channel缓冲区中还有多少数据没有读取.示例如下 c:=make(chan int,20) fmt.Println(&q ...
- c#实现golang 的channel
使用.NET的 BlockingCollection<T>来包装一个ConcurrentQueue<T>来实现golang的channel. 代码如下: public clas ...
- golang的channel实现
golang的channel实现位于src/runtime/chan.go文件.golang中的channel对应的结构是: // Invariants: // At least one of c.s ...
- Golang 入门 : channel(通道)
笔者在<Golang 入门 : 竞争条件>一文中介绍了 Golang 并发编程中需要面对的竞争条件.本文我们就介绍如何使用 Golang 提供的 channel(通道) 消除竞争条件. C ...
- 深入学习golang(2)—channel
Channel 1. 概述 “网络,并发”是Go语言的两大feature.Go语言号称“互联网的C语言”,与使用传统的C语言相比,写一个Server所使用的代码更少,也更简单.写一个Server除了网 ...
- Golang | 简介channel常见用法,完成goroutin通信
今天是golang专题的第14篇文章,大家可以点击上方的专辑回顾之前的内容. 今天我们来看看golang当中另一个很重要的概念--信道.我们之前介绍goroutine的时候曾经提过一个问题,当我们启动 ...
- 【GoLang】golang context channel 详解
代码示例: package main import ( "fmt" "time" "golang.org/x/net/context" ) ...
随机推荐
- python基础举例应用
将下述两个变量的值交换s1='alex's2='SB's1,s2=s2,s1print(s1,s2) 判断下述结果msg1='alex say my name is alex,my age is 73 ...
- mysql常见查询练习题
#建学生信息表student create table student ( sno varchar(20) not null primary key, sname varchar(20) not nu ...
- xshell 评估过期
手头拮据的朋友可以通过下面方法绕过: https://www.netsarang.com/download/down_form.html?code=522 删除XShell. 到英文官网下载页找到XS ...
- HDU 6077 17多校4 Time To Get Up 水题
Problem Description Little Q's clock is alarming! It's time to get up now! However, after reading th ...
- maven项目中的pom.xml
需要配置的内容 1.配置头(自动生成) 2.maven项目的坐标(自动生成) <modelVersion>4.0.0</modelVersion> <groupId> ...
- react native 第三方组件
react native 的成功离不开优秀的第三方组件,以下是我见过的一些优秀或者有用的RN第三方组件 按钮 APSL/react-native-button 导航 react-native-simp ...
- JAVA多线程Thread与Runnable
一.Runnable Runnable为一个之包含一个run方法的接口 public class MyRunnable implements Runnable{ @Override //表示:预示重写 ...
- Unity调用Windows弹框、提示框(确认与否,中文)
Unity调用Windows弹提示框 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
- HDU1272小希的迷宫–并查集
上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了 ...
- python编译hello
pycharm无法找到解释器,将无法编译. 所以在编译之前进行统一设置 点击File,选择settings,点击 添加解释器 最后点击Apply.等待系统配置. 如果我们需要添加新的模块,点击绿色+号 ...