check if a channel is not buffered in go

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.go
c := make(chan int)
if cap(c) == 0 {
    fmt.Println("Channel is unbuffered")
} else {
    fmt.Println("Channel is buffered")
}
128 chars
7 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