Golang Program to Creates Two Channels, One For Even Numbers And Another For Odd Numbers


In this article, we'll make two channels in Go: one for even numbers and another for odd numbers. We are going to send the even numbers to the even channel and the odd numbers to the odd channel.Here we are going to use two different methods: using creating even and odd channels and Sending Even and Odd Numbers to Channels  along with examples to elaborate the concept.

Syntax

close(evenChan)

This is a built in function call used to close a channel. This make sure that no more value will be sent on the channel, and a send operation on the channel.

make() 

The make() method produces a value of the given data structure type that has been initialized.

Algorithm

  • Create two channels, one for even numbers and another for odd numbers, using the make function: evenCh := make(chan int) and oddCh := make(chan int).

  • Implement a goroutine to generate numbers and send them to the appropriate channel. This can be done using a loop that generates numbers and sends them to the corresponding channel based on their parity. For example, using an if statement to check if the number is even or odd: if num%2 == 0 { evenCh <- num } else { oddCh <- num }.

  • In the main function, start the goroutine that generates numbers.

  • Use a separate goroutine to receive even numbers from the evenCh channel. This can be done using a for loop and the channel's range keyword: for num := range evenCh { // process even number }.

  • Within the even number receiver goroutine, process each received even number as desired.

  • Use another separate goroutine to receive odd numbers from the oddCh channel, following a similar approach as in step 4.

  • Optionally, add necessary synchronization mechanisms, such as a WaitGroup, to ensure the program waits for all goroutines to complete.

Example 1

This code creates two channels, evenChan and oddChan, and spawns two goroutines to generate even and odd numbers respectively. The main function then reads from these channels using range loops and prints the received numbers.

package main

import "fmt"

func generateEvenNumbers(evenChan chan<- int) {
   for i := 0; i <= 10; i += 2 {
      evenChan <- i
   }
   close(evenChan)
}

func generateOddNumbers(oddChan chan<- int) {
   for i := 1; i <= 10; i += 2 {
      oddChan <- i
   }
   close(oddChan)
}

func main() {
   evenChan := make(chan int)
   oddChan := make(chan int)

   go generateEvenNumbers(evenChan)
   go generateOddNumbers(oddChan)

   for evenNum := range evenChan {
      fmt.Println("Even Number:", evenNum)
   }

   for oddNum := range oddChan {
      fmt.Println("Odd Number:", oddNum)
   }
}

Output

Even Number: 0
Even Number: 2
Even Number: 4
Even Number: 6
Even Number: 8
Even Number: 10
Odd Number: 1
Odd Number: 3
Odd Number: 5
Odd Number: 7
Odd Number: 9

Example 2

In this code, the sendEvenNumbers function sends even numbers to the evenChan channel, and the sendOddNumbers function sends odd numbers to the oddChan channel. The main function creates the channels, spawns goroutines to send numbers to the channels, and uses a select statement to receive and print the numbers from the channels. The program will output the even and odd numbers in an interleaved manner until all numbers are received from both channels.

package main

import "fmt"

func sendEvenNumbers(evenChan chan<- int) {
   for i := 0; i <= 10; i += 2 {
      evenChan <- i
   }
   close(evenChan)
}

func sendOddNumbers(oddChan chan<- int) {
   for i := 1; i <= 10; i += 2 {
      oddChan <- i
   }
   close(oddChan)
}

func main() {
   evenChan := make(chan int)
   oddChan := make(chan int)

   go sendEvenNumbers(evenChan)
   go sendOddNumbers(oddChan)

   for {
      select {
      case evenNum, ok := <-evenChan:
         if ok {
            fmt.Println("Even Number:", evenNum)
         } else {
            evenChan = nil
         }
      case oddNum, ok := <-oddChan:
         if ok {
            fmt.Println("Odd Number:", oddNum)
         } else {
            oddChan = nil
         }
      }

      if evenChan == nil && oddChan == nil {
         break
      }
   }
}

Output

Odd Number: 1
Odd Number: 3
Odd Number: 5
Odd Number: 7
Odd Number: 9
Even Number: 0
Even Number: 2
Even Number: 4
Even Number: 6
Even Number: 8
Even Number: 10

Conclusion

In this article we have looked at how we can make use of channels in Go to partitioned even and odd numbers into two distinctive channels. By utilizing Goroutines and channels, we will send and get information at the same time, making our program efficient. Channels give an effective component for communication and synchronization between Goroutines, empowering the advancement of concurrent and parallel applications in Go.

Updated on: 20-Jul-2023

544 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements