Golang program to detect a unidirectional receiving channel of strings and a sending channel of integers.


Unidirectional channel is a type of channel that can only be used for either receiving or sending the data.In this article, we will create a golang program to detect a receiving channel that consist of string and a sending channel that has integers in it. Here we will learn how to utilize unidirectional channels to receive strings and send integers using unbuffered and buffered channels, select statements and using a struct to combine channels.

Syntax

ch := make(chan string)

Creates a channel to handle strings.

ch <- "Hello, World!"

To send the string value

message := <-ch

To receive a string value

<-chan string

This represents the unidirectional receiving channel of strings

Algorithm

  • import the "fmt" and "sync" packages.

  • Define the "processString" and "processData" functions.

  • Use the defer line inside the "processData" method to postpone the call to wg.Done(). When the goroutine is finished, this will inform the WaitGroup.

  • Using a range loop, iterate over the values received from the input channel (in).

  • Create an input channel called "input" and an output channel called "output" in the main function.

  • Make a WaitGroup named "wg" and use it to add 1 to its counter. Now Start Goroutines

  • And Print Out the Processed Values

Example 1: Using UnBuffered channels

For go-routines to communicate between each other, unbuffered channels are one of the effective ways. By synchronizing the actions of the transmitter and receiver goroutines by using unbuffered channels, it is ensured that each value is processed before the next value is transmitted.

package main

import (
   "fmt"
   "sync"
)

func processString(value string) int {
   return len(value)
}

func processData(in <-chan string, out chan<- int, wg *sync.WaitGroup) {
   defer wg.Done()

   for value := range in {
      processedValue := processString(value)

      out <- processedValue
   }
}

func main() {
   input := make(chan string)
   output := make(chan int)

   var wg sync.WaitGroup
   wg.Add(1) // Increment the WaitGroup counter

   go processData(input, output, &wg)

   go func() {
      defer close(input)

      input <- "123"
      input <- "456"
      input <- "789"
   }()

   go func() {
      defer close(output)

      for processedValue := range output {
         fmt.Println(processedValue)
      }
   }()

   wg.Wait()
}

Output

3
3
3

Example 2: Using select statement

Select statement enables us to manage several channel activities at once. Select statements make sure that the program proceeds with the operation which is ready to process, which results in avoiding the unnecessary waiting time.

package main

import (
   "fmt"
   "sync"
)
func processString(value string) int {

   return len(value)
}
func processData(in <-chan string, out chan<- int, wg *sync.WaitGroup) {
   defer wg.Done() 

   for value := range in {
      processedValue := processString(value)

      out <- processedValue
   }
}
func main() {
   input := make(chan string)
   output := make(chan int, 10) 

   var wg sync.WaitGroup
   wg.Add(1) 

   go processData(input, output, &wg)

   input <- "123"
   input <- "456"
   input <- "789"
   close(input)

   go func() {
      for processedValue := range output {
         fmt.Println(processedValue)
      }
   }()

   wg.Wait()

   close(output)
}

Output

3
3
3

Conclusion

In this article we have discussed how to write a program that uses unidirectional channels to receive text and transmit integers. You can easily communicate across goroutines by Go's built-in concurrency support and channels, enabling efficient and concurrent data processing.

Updated on: 05-Jul-2023

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements