Golang Program to Creates a Buffered Channel of Size 5 And Sends 10 Integers to The Channel Using a Loop


In this article, we focus on making a buffered channel of size 5 and sending 10 integers to the channel using a loop. The program illustrates how to make and utilize a buffered channel to send large values.Here we are going to use two different methods: by Sending integers to the channel using a loop and by creating a buffered channel along with examples to elaborate the concept.

Syntax

ch := make(chan int, bufferSize)

The Syntax ch := make(chan int, bufferSize) creates a buffered channel ch of type int with a specified buffer size, allowing the sender to send multiple values without blocking until the buffer is full.

sync.waitgroup()

the WaitGroup struct type provides methods for coordinating the execution of many goroutines. It works by allowing you to add goroutines to the wait group and then wait for them all to complete before proceeding with the programme.

wg.done()

The function wg.Done() decrements the WaitGroup's number, signalling that the relevant goroutine has finished its task. This is necessary to guarantee that the WaitGroup waits for all goroutines to complete before allowing the programme to continue.

Algorithm

  • Create a buffered channel of integers with a size of 5 using the make function − ch := make(chan int, 5).

  • Start a loop that iterates 10 times to send the integers to the channel.

  • Inside the loop, use the channel's <- operator to send each integer to the channel: ch <- value where value is the current integer.

  • After sending all 10 integers, close the channel to indicate that no more values will be sent: close(ch).

  • Implement a separate goroutine to receive the values from the channel. This can be done using a for loop and the channel's range keyword: for num := range ch { // process num }.

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

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

Example 1

In this code, we create a buffered channel ch of size 5 using make(chan int, 5). Then, we use a loop to send 10 integers to the channel. Finally, we close the channel and read the values from the channel using a range loop, printing each value.

package main

import (
   "fmt"
   "sync"
)

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

   var wg sync.WaitGroup

   wg.Add(1)

   go func() {
      defer wg.Done()

      // Reading and printing the values from the channel
      for value := range ch {
         fmt.Println(value)
      }
   }()

   for i := 0; i < 10; i++ {
      ch <- i
   }

   close(ch)

   wg.Wait()
}

Output

0
1
2
3
4
5
6
7
8
9

Example 2

In this code, we create a buffered channel ch with a size of 5. Then, we use a loop to send 10 integers to the channel by using the channel send operator <-. After sending all the values, we close the channel using the close function to indicate that no more values will be sent. Finally, we iterate over the channel using a for loop and use the range expression to receive values from the channel one by one. The received values are then printed using the fmt.Println function.

package main

import (
   "fmt"
   "sync"
)

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

   var wg sync.WaitGroup

   wg.Add(1)
   go func() {
      defer wg.Done()
      for i := 0; i < 10; i++ {
         ch <- i
      }
      close(ch)
   }()

   wg.Add(1)
   go func() {
      defer wg.Done()
      for value := range ch {
         fmt.Println(value)
      }
   }()

   wg.Wait()
}

Output

0
1
2
3
4
5
6
7
8
9

Conclusion

In this Golang program, we have effectively made a buffered channel with a measure of 5 and sent 10 integrability to the channel employing a circle. Buffered channels permit a certain number of values to be sent without blocking the sender, giving a way to handle concurrent operations productively. By following the calculation, we are able to utilize channels successfully in our programs to encourage communication between goroutines.

Updated on: 20-Jul-2023

52 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements