Unidirectional Channel Patterns
Common Unidirectional Channel Design Patterns
Unidirectional channels provide powerful mechanisms for controlled communication between goroutines, enabling more predictable and safer concurrent programming.
Pipeline Pattern
graph LR
A[Input] --> B[Stage 1]
B --> C[Stage 2]
C --> D[Output]
Implementation Example
func generateNumbers(max int) <-chan int {
ch := make(chan int)
go func() {
for i := 1; i <= max; i++ {
ch <- i
}
close(ch)
}()
return ch
}
func squareNumbers(input <-chan int) <-chan int {
output := make(chan int)
go func() {
for num := range input {
output <- num * num
}
close(output)
}()
return output
}
func main() {
numbers := generateNumbers(5)
squared := squareNumbers(numbers)
for result := range squared {
fmt.Println(result)
}
}
Fan-Out Pattern
graph LR
A[Single Channel] --> B[Worker 1]
A --> C[Worker 2]
A --> D[Worker 3]
Implementation Example
func fanOutWorker(id int, jobs <-chan int, results chan<- int) {
for job := range jobs {
result := job * 2
results <- result
}
}
func fanOutProcess(jobCount int) {
jobs := make(chan int, jobCount)
results := make(chan int, jobCount)
// Start workers
for w := 1; w <= 3; w++ {
go fanOutWorker(w, jobs, results)
}
// Send jobs
for j := 1; j <= jobCount; j++ {
jobs <- j
}
close(jobs)
// Collect results
for a := 1; a <= jobCount; a++ {
<-results
}
}
Worker Pool Pattern
Pattern Component |
Description |
Input Channel |
Receives tasks |
Worker Channels |
Process tasks concurrently |
Result Channel |
Collects processed results |
Implementation Example
func worker(id int, jobs <-chan int, results chan<- int) {
for job := range jobs {
fmt.Printf("Worker %d processing job %d\n", id, job)
results <- job * 2
}
}
func workerPool(jobCount, workerCount int) {
jobs := make(chan int, jobCount)
results := make(chan int, jobCount)
// Create worker pool
for w := 1; w <= workerCount; w++ {
go worker(w, jobs, results)
}
// Send jobs
for j := 1; j <= jobCount; j++ {
jobs <- j
}
close(jobs)
// Collect results
for a := 1; a <= jobCount; a++ {
<-results
}
}
Best Practices
- Use send-only and receive-only channel directions
- Close channels when no more data will be sent
- Implement proper error handling
- Consider buffered channels for performance optimization
At LabEx, we emphasize using unidirectional channels to create more predictable and maintainable concurrent Go applications.