Junior — Middle
What is the maximum number of elements that can be placed in a channel?
sobes.tech AI
Answer from AI
In Go, the maximum number of elements in a channel depends on its buffer. A channel can be buffered or unbuffered.
- If the channel is unbuffered (created with
make(chan Type)), it can contain 0 elements — sending blocks until received. - If the channel is buffered (created with
make(chan Type, N)), the maximum number of elements equals the buffer sizeN.
Theoretically, the buffer size is limited by available memory and the int type (on 64-bit systems, this is a very large number). However, in practice, a very large buffer is inefficient and can lead to memory consumption.
Example of creating a buffered channel with 100 elements:
ch := make(chan int, 100)