Channel methods
V exposes some public methods to control a channel's behavior. These methods include the following:
try_push()
try_pop()
close()
Except for the close()
method, the try_push()
and try_pop()
methods have a return value, which is a built-in enum type called ChanState
. The ChanState
enum has three enum values:
not_ready
closed
success
Performing try_push()
or try_pop()
on a channel can return one of the three aforementioned states. In this section, we will learn how to work with the try_push()
function with buffered and unbuffered channels and then we cover how to work with the try_pop()
and close()
methods.
Using try_push() on unbuffered channels
try_push()
gracefully pushes data into the channel and returns the status in the form of an enum value from ChanState
. The try_push()
method accepts values of the type that the channel accepts. For an unbuffered channel, the try_push()
operation returns a .not_ready
value...