Golang program to takes a receiving channel of integers and a sending channel of strings


In this article, we will learn how to develop a golang program that takes a receiving channel as integer and sends a channel of strings. Here we will be using a concurrent approach that involves using the multiple goroutines to perform transformation concurrently as well as using select statements with timeout.

Syntax

channel_name := make(chan Type)

To create a channel.

channel_name <- element

To send value to a channel, element is the value.

variable := <-channel_name

Recieving value from the channel, variable is the received value.

Algorithm

Make a method called transformIntToString that accepts a channel of integers for receiving a channel of strings for transmitting (send), and the quantity of worker goroutines.

Example 1

  • Create the numWorkers goroutines by starting a loop.

  • Wait for all worker goroutines to complete using wg after the loop producing goroutines.Wait().

  • To indicate that no additional strings will be transmitted, close the send channel.

  • In the primary function −

    • Make a receiving channel called receive and a sending channel called send.

    • Begin a goroutine to populate the receive channel with numbers ranging from 1 to 10, then exit.

    • Begin another goroutine with three worker goroutines to perform the transformIntToString method.

  • To receive strings from the transmit channel, use a loop.

  • Use fmt to print each incoming string str.Println(str).

Example 2

  • Start an unlimited loop to process channel activities indefinitely.

  • Use the select statement to choose between the available cases: receiving from receive channel or receiving a timeout from time.After.

  • In the main function −

    • Make a receiving channel called receive and a sending channel called send.

    • Begin a goroutine to populate the receive channel with numbers ranging from 0 to 9, then end it.

    • Begin another goroutine with a delay of 2 seconds to perform the transformIntToString function.

    • To receive strings from the transmit channel, use a loop.

  • Print each incoming string str using fmt.Println(str).

Example 1: Concurrent Approach

We use this approach while processing a large number of integers. In this method we can process the integers concurrently and send them to the string to the sending channel as soon as they are prepared by concurrent goroutines.

package main

import (
   "fmt"
   "strconv"
   "sync"
)

func transformIntToString(receive <-chan int, send chan<- string, numWorkers int) {
   var wg sync.WaitGroup
   wg.Add(numWorkers)

   for i := 0; i < numWorkers; i++ {
      go func() {
         defer wg.Done()
         for num := range receive {
            str := strconv.Itoa(num)
            send <- str
         }
      }()
   }

   wg.Wait()
   close(send)
}

func main() {
   receive := make(chan int)
   send := make(chan string)

   go func() {
      for i := 1; i <= 10; i++ {
         receive <- i
      }
      close(receive)
   }()

   go transformIntToString(receive, send, 3)

   for str := range send {
      fmt.Println(str)
   }
}

Output

1
2
3
4
5
6
7
8
9
10

Example 2: Select Statement with timeout

We will introduce a timeout mechanism for the transformation process by using the select statement in go. This method allows us to minimize the waiting time while receiving the value from the receiving channel.

package main

import (
   "fmt"
   "strconv"
   "time"
)

func transformIntToString(receive <-chan int, send chan<- string, timeout time.Duration) {
   for {
      select {
      case num, ok := <-receive:
         if !ok {
            close(send)
            return
         }
         str := strconv.Itoa(num)
         send <- str
      case <-time.After(timeout):
         close(send)
         return
      }
   }
}

func main() {
   receive := make(chan int)
   send := make(chan string)

   go func() {
      for i := 0; i <= 9; i++ {
         receive <- i
      }
      close(receive)
   }()

   go transformIntToString(receive, send, time.Second*2)

   for str := range send {
      fmt.Println(str)
   }
}

Output

0
1
2
3
4
5
6
7
8
9

Conclusion

In this article we have discussed the two approaches to taking a receiving channel of integers and sending channels of string, concurrent approach and the select statement method.These methods allow goroutines to communicate in a secure and efficient manner.

Updated on: 06-Jul-2023

111 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements