The Go language type system provides a one-directional channel type. As the name suggests, a one-directional channel is only used for writing or reading data. Of course, the channel itself must support reading and writing at the same time, otherwise it will be useless at all.
If a channel can only read data, it will definitely be empty because you don't have the chance to write data into it. Similarly, if a channel only allows data to be written, even if it is written, it has no meaning, because there is no way to read the data inside. The so-called one-way channel concept is actually just a restriction on the use of channel.
Declaration format for one-way channel
When we pass a channel variable to a function, we can limit the operations that can be used in the function by specifying it as a one-way channel variable, such as only writing data into this channel, or reading data from this channel.
The declaration of a one-way channel variable is very simple. The channel type that can only be written to data is chan<-, and the channel type that can only be read to data is <-chan, and the format is as follows:
var channel instance chan<- Element type // Channel that can only write data
var channel instance <-chan element type // Channel that can only read data
- Element type: The element type that the channel contains.
- Channel instance: declared channel variable.
Examples of use of one-way channels
The sample code is as follows:
ch := make(chan int) // Declare a channel type that can only be written to data, and assign a value to chvar chSendOnly chan<- int = ch //Declare a channel type that can only read data, and assign it to chvar chRecvOnly <-chan int = ch
In the above example, chSendOnly can only write data. If you try to read data, the following error will occur:
invalid operation: <-chSendOnly (receive from send-only type chan<- int)
Similarly, chRecvOnly cannot write data.
Of course, when creating a channel using make, you can also create a write-only or read-only channel:
ch := make(<-chan int) var chReadOnly <-chan int = ch <-chReadOnly
The above code compiles normally and runs correctly. However, a channel that cannot write data and can only read is meaningless.
One-way channel in time package
The timer in the time package will return a timer instance, the code is as follows:
timer := ()
The Timer type of timer is defined as follows:
type Timer struct { C <-chan Time r runtimeTimer }
In line 2, the type of channel C is a one-way channel that can only be read. If channel direction constraints are not performed here, once data is written to the channel externally, it will cause confusion in other places where the timer is used.
Therefore, one-way channels are conducive to the rigor of the code interface.
Close channel
Close channel is very simple, just use the built-in close() function in Go:
close(ch)
After introducing how to close a channel, we have another question: How to determine whether a channel has been closed? We can use multiple return values when reading:
x, ok := <-ch
This usage is similar to the process of obtaining value by pressing a key in map. You only need to look at the second bool return value. If the return value is false, it means that ch has been closed.
This is the end of this article about the implementation of Go unidirectional channels. For more relevant Go unidirectional channels, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!