我的频道还剩多少容量?

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

How much capacity is left in my channel?

问题

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

例如:

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

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

For example

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

答案1

得分: 6

例如,

  1. package main
  2. import "fmt"
  3. func main() {
  4. a := make(chan bool, 5)
  5. a <- true
  6. fmt.Println(cap(a) - len(a))
  7. }

输出:

  1. 4
英文:

For example,

  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. a := make(chan bool, 5)
  5. a &lt;- true
  6. fmt.Println(cap(a) - len(a))
  7. }

Output:

  1. 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:

确定