channel buffer可以事先分配大小,但是这些是需要占用内存的,事先分配几G内存给一个channel很浪费资源的,所以怎样创建一个无限的channel buffer呢?比较naive的写法就是把东西放进一个队列里,然后时刻检查队列的大小,比如说: for { if len(queue) != 0 { data = queue.front() out <- data } else { time.sleep(time.Duration(1) * time.Microsecond) } } 但…
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…
可以通过内建函数len查看channel中元素的个数. 内建函数len的定义如下: func len(v Type) int The len built-in function returns the length of v, according to its type: Array: the number of elements in v.数组中元素的个数 Pointer to array: the number of elements in *v (even if v is nil).数组中…