As we know that a channel is a medium of communication between concurrently running goroutines so that they can send and receive data to each other. By default a channel is bidirectional but you can create a unidirectional channel also. A channel that can only receive data or a channel that can only send data is the unidirectional channel. The unidirectional channel can also create with the help of make() function as shown below:
// Only to receive data c1:= make(<- chan bool) // Only to send data c2:= make(chan<-bool
<-chan string chan<- string
Converting Bidirectional Channel into the Unidirectional Channel
In Go language, you are allowed to convert a bidirectional channel into the unidirectional channel, or in other words, you can convert a bidirectional channel into a receive-only or send-only channel, but vice versa is not possible. As shown in the below program:
Use of Unidirectional Channel: The unidirectional channel is used to provide the type-safety of the program so, that the program gives less error. Or you can also use a unidirectional channel when you want to create a channel that can only send or receive data.
- Channel in Golang
- Buffered Channel in Golang
- How to find the capacity of Channel, Pointer and Slice in Golang?
- How to find the Length of Channel, Pointer, Slice, String and Map in Golang?
- Zero value in Golang
- Composition in Golang
- Packages in Golang
- Rune in Golang
- What is Regex in Golang?
- How to copy a map to another map in Golang?
- Methods in Golang
- Methods With Same Name in Golang
- Hello World in Golang
- Interfaces in Golang
- Using WaitGroup in Golang
- How to use Ellipsis (...) in Golang?
- Recover in Golang
- Pointers in Golang
- Closures in Golang
- Inheritance in GoLang
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : amit bhaira