Sobes.tech
Junior — Middle

Kuidas saab määrata kanali praeguse järjekorra pikkuse?

sobes.tech AI

Vastus AI-lt

In Go, the length of the current queue in a channel can be determined using the built-in len() function. It returns the number of elements currently in the channel's 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 print 2

Important: len() works only for buffered channels; for unbuffered channels, it will always be 0.