Programming Languages
Go
Subjective
Oct 04, 2025
What are the different types of channels and their use cases?
Detailed Explanation
Channel Types and Patterns:
• Unbuffered: Synchronous communication
• Buffered: Asynchronous with capacity
• Directional: Send-only, receive-only
• Select with channels for multiplexing
Patterns:
• Fan-out: Distribute work
• Fan-in: Collect results
• Pipeline: Chain operations
• Worker pools: Limit concurrency
• Cancellation: Context-based
Example:
// Directional channels
func sender(ch chan<- int) { ch <- 42 }
func receiver(ch <-chan int) { v := <-ch }
// Pipeline
func pipeline() <-chan int {
out := make(chan int)
go func() {
defer close(out)
for i := 0; i < 10; i++ {
out <- i * i
}
}()
return out
}
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts