Golang program to use a unidirectional channel to send integers from 1 to 10 to a receiving function


In this go language article we are going to use a unidirectional channel to send integers from 1 to 10 to a receiving channel. We will be using a basic channel, a buffered channel with fixed capacity as well as using a select statement for non-blocking channel operation.

Syntax

ch := make (chan int)

To create an unbuffered channel

Value := <-ch

Pseudo

To receive value from a channel

select {
case ch <- value:
   // Code to execute when sending to the channel is possible
default:
   // Code to execute when sending to the channel is not possible (channel full)
}

Use select statement for non blocking channel operation

Algorithm

  • For communication between the sender and receiver goroutines, create an int channel.

  • Create a function named “sendNumbers” that sends numbers from 1 to 10 to a channel (ch) using the parameter.

  • To iterate from 1 to 10, use a loop. Use the - operator inside the loop to transmit each integer to the channel.

  • Create the function “receiveNumbers”, which accepts the argument ch and receives and outputs integers from the channel.

  • Till the channel is closed, iterate over it using a loop and the range keyword.

  • Take in each integer from the channel and print it inside the loop.

  • In main function() −

    • Make an int-type buffered channel with a capacity of 5.

    • Launch the “goroutine” that implements the “sendNumbers” function, passing the channel as an argument.

    • Use the channel as a parameter when calling the “receiveNumbers” method.

  • The transmitter goroutine will send integers to the channel while the receiver goroutine will receive and output those integers concurrently while the programme runs.

Example 1: Using Basic channel communication

This example includes setting up a basic channel and sending the integers one by one to the receiving function. Here the sender goes routine, sends integers from 1 to 10 to a channel, and the receiving goroutine accepts it and prints it.

package main

import "fmt"

func sendNumbers(ch chan<- int) {
   for i := 1; i <= 10; i++ {
      ch <- i
   }
   close(ch)
}

func receiveNumbers(ch <-chan int) {
   for num := range ch {
      fmt.Println(num)
   }
}

func main() {
   ch := make(chan int)
   go sendNumbers(ch)
   receiveNumbers(ch)
}

Output

1
2
3
4
5
6
7
8
9
10

Example 2: Using Buffered channel having fixed Capacity

This example involves setting up a buffered channel with fixed capacity, and sending multiple integers at once before the receiving function starts processing them. We have defined the channel with capacity 5 that means the sender can send 5 integers without blocking.

package main

import "fmt"

func sendNumbers(ch chan<- int) {
   for i := 1; i <= 10; i++ {
      ch <- i
   }
   close(ch)
}

func receiveNumbers(ch <-chan int) {
   for num := range ch {
      fmt.Println(num)
   }
}

func main() {
   ch := make(chan int, 5) 
   go sendNumbers(ch)
   receiveNumbers(ch)
}

Output

1
2
3
4
5
6
7
8
9
10

Example 3: Using select statement for non blocking channel operation

This method makes use of a select statement and a buffered channel with a capacity range of 3 to handle the operation.

package main

import (
   "fmt"
   "time"
)

func sendNumbers(ch chan<- int) {
   for i := 1; i <= 10; i++ {
      select {
      case ch <- i:
         fmt.Println("Sent:", i)
      default:
         fmt.Println("Channel is full!")
      }
      time.Sleep(time.Millisecond * 700) // Pause for demonstration purposes
   }
   close(ch)
}

func receiveNumbers(ch <-chan int) {
   for num := range ch {
      fmt.Println("Received:", num)
      time.Sleep(time.Millisecond * 500) // Pause for demonstration purposes
   }
}

func main() {
   ch := make(chan int, 3)
   go sendNumbers(ch)
   receiveNumbers(ch)
}

Output

Sent: 1 
Received: 1 
Sent: 2 
Received: 2 
Sent: 3 
Received: 3 
Sent: 4 
Received: 4 
Sent: 5 
Received: 5 
Sent: 6 
Received: 6 
Sent: 7 
Received: 7 
Sent: 8 
Received: 8 
Sent: 9 
Received: 9 
Sent: 10 
Received: 10 

Conclusion

In this article we have discussed three distinct approaches for transmitting integers ranging from 1 to 10 to a receiving function over unidirectional channels. Using these approaches, you may take use of the power of Go's concurrent programming model to create strong, scalable applications.

Updated on: 05-Jul-2023

47 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements