Last active
August 5, 2021 15:34
-
-
Save mantyr/961cdbaf5f33d609ec4290cdc26a18c8 to your computer and use it in GitHub Desktop.
Golang len(chan) - размер загруженности очереди (chan) не читая из неё
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
c := make(chan int, 100) | |
for i := 0; i < 34; i++ { | |
c <- 0 | |
} | |
fmt.Println(len(c)) // print "34" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Оказывается можно узнать размер загруженности очереди не читая из неё | |
http://stackoverflow.com/questions/13003749/number-of-elements-in-a-channel | |
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). | |
Slice, or map: the number of elements in v; if v is nil, len(v) is zero. | |
String: the number of bytes in v. | |
Channel: the number of elements queued (unread) in the channel buffer; if v is nil, len(v) is zero. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment