Understanding Buffered Channels in Golang with sample problems
How do you work over a buffered channel ? this question while we learn about the concept channel ? Before jumping into the buffered channel, we need to understand what the channel is. Introduction to Channels in Go Channels in Go provide a medium for communication between two or more goroutines. Goroutines are lightweight threads managed by the Go runtime scheduler, allowing concurrent execution. Types of Channels in Go Buffered Channel – A sender can send data until the buffer is full. Once full, it blocks further sends until space is available. Used for asynchronous communication. Unbuffered Channel – A sender can only send data if a receiver is ready. The sender blocks until the receiver reads the value. Used for synchronous communication. Working with Buffered Channels Let's take an array and use goroutines with buffered channels to compute: The sum of the array The largest element The second-largest element ` package main import ( "fmt" "sync" ) func secondLargest(arr []int, ch chan int, wg *sync.WaitGroup) lar { secnd = lar lar = v } else if v < lar && v > secnd { secnd = v } } ch

How do you work over a buffered channel ? this question while we learn about the concept channel ?
Before jumping into the buffered channel, we need to understand what the channel is.
Introduction to Channels in Go
Channels in Go provide a medium for communication between two or more goroutines. Goroutines are lightweight threads managed by the Go runtime scheduler, allowing concurrent execution.
Types of Channels in Go
Buffered Channel – A sender can send data until the buffer is full. Once full, it blocks further sends until space is available.
Used for asynchronous communication.
Unbuffered Channel – A sender can only send data if a receiver is ready. The sender blocks until the receiver reads the value.
Used for synchronous communication.
Working with Buffered Channels
Let's take an array and use goroutines with buffered channels to compute:
The sum of the array
The largest element
The second-largest element
`
package main
import (
"fmt"
"sync"
)
func secondLargest(arr []int, ch chan int, wg *sync.WaitGroup) <-chan int {
// Decreases the counter by 1 when a goroutine finishes execution
defer wg.Done()
lar := arr[0]
secnd := -1
for _, v := range arr {
if v > lar {
secnd = lar
lar = v
} else if v < lar && v > secnd {
secnd = v
}
}
ch <- secnd
return ch
}
func sumOfArray(arr []int, ch chan int, wg *sync.WaitGroup) <-chan int {
// Decreases the counter by 1 when a goroutine finishes execution
defer wg.Done()
sum := 0
for _, v := range arr {
sum += v
}
ch <- sum
return ch
}
func largestElement(arr []int, ch chan int, wg *sync.WaitGroup) <-chan int {
// Decreases the counter by 1 when a goroutine finishes execution
defer wg.Done()
largest := arr[0]
for _, v := range arr {
if v > largest {
largest = v
}
}
ch <- largest
return ch
}
func main() {
//Buffered channel of size 3
ch := make(chan int, 3)
//wait for a group of goroutines to finish execution
var wg sync.WaitGroup
arr := []int{2, 3, 4, 50, 6, 70, 8, 9}
//Increases the counter by the specified number of goroutines.
wg.Add(3)
go secondLargest(arr, ch, &wg)
fmt.Println("Second largest", <-ch)
go sumOfArray(arr, ch, &wg)
fmt.Println("Sum of array", <-ch)
go largestElement(arr, ch, &wg)
fmt.Println("largest element of array", <-ch)
go func() {
close(ch)
//Blocks execution until the counter becomes zero.
wg.Wait()
}()
}
`
Buffered Channel Diagram (Capacity = 3)
Goroutines Send Data
┌───────────────────────────────┐
│ Goroutine 1 -> ch <- 50 │
│ Goroutine 2 -> ch <- 70 │
│ Goroutine 3 -> ch <- 100 │
└───────────────────────────────┘
Buffered Channel (Capacity = 3)
┌───────────┬───────────┬───────────┐
│ 50 │ 70 │ 100 │ (Buffered)
└───────────┴───────────┴───────────┘
Main Goroutine Reads Data
┌───────────────────────────────┐
│ fmt.Println(<-ch) → 50 │
│ fmt.Println(<-ch) → 70 │
│ fmt.Println(<-ch) → 100 │
└───────────────────────────────┘