Golang Program to Creates a Unidirectional Sending Channel and Passes it to a Function that Returns a Unidirectional Receiving Channel


A unidirectional sending channel is used to send the values to the channel, while the unidirectional receiving channel is used to receive the data from the channel. These channels are used in concurrent data processing, data sharing and more. In this golang article we are going to create a program to create a unidirectional sending and pass it to a function that returns a unidirectional receiving channel using function parameters, type conversion as well as channel composition.

Algorithm

  • Define the sender Function that accepts a send-only channel named “ch” of type integer.

  • Use the close function to close the channel.

  • Define receiver function that accepts a receive-only channel of type int and send only channel of type bool.

  • Use the range keyword to iterate over the values received from the “ch” channel.

  • Use the “fmt.println” function to print the received value.

  • Once the channel is closed, send true value to the “done” channel to mark the completion of the receiver function.

  • In the main function():

    • Create a channel named “sendch” of type integer using make function.

    • Create a “sendonly” channel named “done” of type Boolean using the make function.

    • Wait until the receiver is finished by receiving the value from done channel.

    • Now print “program is terminated” using “fmt.println” function

Example 1

This example creates two functions, for creating unidirectional sending channels as well as accepting unidirectional sending channels.The following code demonstrates the use of the unidirectional channel for sending and receiving data between goroutines.

package main

import "fmt"

func sender(ch chan<- int) {
   ch <- 32
   close(ch)
}

func receiver(ch <-chan int, done chan<- bool) {
   for val := range ch {
      fmt.Println(val)
   }
   done <- true
}

func main() {
   sendCh := make(chan int) 
   done := make(chan bool)  

   go sender(sendCh)
   go receiver(sendCh, done) 

   <-done // Wait for receiver to finish

   fmt.Println("Program is terminated.")
}

Output

32
Program is terminated.

Example 2

This Example involves creating a bidirectional channel and uses type conversion to make it a unidirectional sending/receiving channel.The below code uses channels to coordinate the sending and receiving data between goroutines.

package main

import (
   "fmt"
   "sync"
)

func sender(ch chan<- int, wg *sync.WaitGroup) {
   ch <- 32
   close(ch)
   wg.Done()
}

func receiver(ch <-chan int, wg *sync.WaitGroup) {
   for val := range ch {
   fmt.Println(val)
   }
   wg.Done()
}

func main() {
   ch := make(chan int)

   var wg sync.WaitGroup
   wg.Add(2)

   go sender(ch, &wg)
   go receiver(ch, &wg)

   wg.Wait()

   fmt.Println("Program is terminated.")
}

Output

32
Program is terminated. 

Method 3: Using Channel Composition

Example

In this example, the “<-“ operator is used compose channels. It enables to create a composite channel to combine sending and receiving channel type. The below code shows the usage of the send only channel and receive only channel in go language.

package main

import (
   "fmt"
   "sync"
)

type SendOnlyChannel chan<- int
type ReceiveOnlyChannel <-chan int

func sender(ch chan<- int, wg *sync.WaitGroup) {
   ch <- 22
   close(ch)
   wg.Done()
}

func receiver(ch <-chan int, wg *sync.WaitGroup) {
   for val := range ch {
      fmt.Println(val)
   }
   wg.Done()
}

func main() {
   sendCh := make(chan int) 

   var sendOnly SendOnlyChannel = sendCh
   var recvOnly ReceiveOnlyChannel = sendCh

   var wg sync.WaitGroup
   wg.Add(2)

   go sender(sendOnly, &wg)
   go receiver(recvOnly, &wg)

   wg.Wait()

   fmt.Println("Program is terminated.")
}

Output

22
Program is terminated.

Conclusion

In this article we have discussed that the unidirectional channels are an effective tool for controlling data flow and preventing unauthorized use. We have explored three different methods including type conversion, channel composition as well as type conversion.

Updated on: 13-Jul-2023

52 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements