我的频道还剩多少容量?

huangapple go评论74阅读模式
英文:

How much capacity is left in my channel?

问题

有没有办法在Go中查看我的异步通道中剩余的存储空间?

例如:

a := make(chan bool, 5)
a <- true
fmt.Printf(a.capazity())
英文:

Is there a way to see how much storage is left in my asynchronous channel in Go?

For example

a chan uint=make(chan bool, 5)
a&lt;-true
fmt.Printf(a.capazity())

答案1

得分: 6

例如,

package main

import "fmt"

func main() {
    a := make(chan bool, 5)
    a <- true
    fmt.Println(cap(a) - len(a))
}

输出:

4
英文:

For example,

package main

import &quot;fmt&quot;

func main() {
	a := make(chan bool, 5)
	a &lt;- true
	fmt.Println(cap(a) - len(a))
}

Output:

4

huangapple
  • 本文由 发表于 2013年6月27日 20:39:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/17343445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定