Here's a simple code snippet to check if a channel is not full using the select
statement in Go:
main.go263 chars11 lines
In this code, a select
statement is used to try to put a value in the channel. If the channel is not full, the value is put in and the function returns true
. If the channel is full, the default case is triggered, the value is discarded, and the function returns false
.
Note that the <-
operator is used with the channel to receive a value from the channel, while the ->
operator is used to put a value into the channel. By using the ch <- 1
syntax, we are attempting to put a value into the channel without blocking. If the channel is full, this will not be possible and the default case will be triggered.
gistlibby LogSnag