To check if a channel is unbuffered or non-buffered, we can use the cap
function which returns the maximum capacity of the channel. If the channel has no buffer, cap
will return zero.
Here is an example:
main.go128 chars7 lines
In this example, we create a new channel c
of type int
using the make
keyword. We then use the cap
function to check if c
has a maximum capacity of zero. If this is true, we print out that the channel is unbuffered, otherwise we print out that the channel is buffered.
gistlibby LogSnag