Junior — Middle
How can the current length of a queue in a channel be determined?
sobes.tech AI
Answer from AI
In Go, the length of the current queue in a channel can be determined using the built-in function len(). It returns the number of elements currently in the channel buffer (i.e., the number of messages waiting to be received). For example:
ch := make(chan int, 5)
ch <- 1
ch <- 2
fmt.Println(len(ch)) // Will output 2
Important: len() works only for buffered channels; for unbuffered channels, it will always be 0.