Search⌘ K
AI Features

Directional Nature of the Channel

Explore how Go channels can be directional by using bidirectional, send-only, or receive-only channels. Understand why unidirectional channels help reduce errors and make your concurrency code more robust through practical examples.

Bidirectional nature of the channel

Channels are bidirectional. That is, we can both send and receive data using channels. Let’s look at the working code for a bidirectional channel. the bidirectional channel:

Go (1.6.2)
package main
import "fmt"
func printValue(goChannel chan int) {
fmt.Println("Value inside the channel is ", <-goChannel)
goChannel <- 1
}
func main() {
goChannel := make(chan int)
go printValue(goChannel)
goChannel <- 2
fmt.Println("Value inside the channel is ", <-goChannel)
}

We can send to and receive from the printValue function ...