This project demonstrates various channel patterns and techniques for effective concurrency in Go. It provides practical examples showing how channels work in different scenarios.
Channels are a core feature of Go's concurrency model. They provide a way for goroutines to communicate with each other and synchronize their execution without explicit locks or condition variables.
There are multiple ways to run the examples:
go run main.go# Run all examples
make run
# Build the binary
make build
# Run a specific learning topic
make learn TOPIC=basicThe program will automatically cycle through all examples with a brief pause between each demonstration.
This project uses GitHub Actions for continuous integration. The workflow automatically builds and tests the code on every push to the main branch or pull request.
The CI workflow performs the following steps:
- Sets up Go environment
- Builds the project
- Runs tests
- Verifies the binary execution
You can see the workflow configuration in .github/workflows/go.yml.
The program includes a special learning mode that provides detailed step-by-step explanations of each channel pattern before demonstrating it.
To see available learning topics:
go run main.go -listTo run the learning mode for a specific topic:
go run main.go -learn basic
go run main.go -learn buffered
go run main.go -learn sync
go run main.go -learn select
go run main.go -learn errors
go run main.go -learn pipelineLearning mode features:
- Detailed explanations of concepts
- Code examples with expected output
- Additional notes and best practices
- Live demonstration after the explanations
-
Basic Channel Communication
- Simple send/receive operations
- Unidirectional channels
- Channel iteration and closing
-
Buffered vs Unbuffered Channels
- Differences in behavior
- Buffer capacity and blocking
-
Channel Synchronization
- Simple synchronization
- Worker pool pattern
- Fan-out/fan-in pattern
- Using channels as semaphores
-
Select Statement
- Waiting on multiple channels
- Timeouts
- Non-blocking operations with default case
- Multiplexing multiple channels
- Detecting when a channel is closed
-
Error Handling with Channels
- Result channels
- Error channels
- Panic recovery in goroutines
- Context-based cancellation
- Deadlock avoidance strategies
-
Practical Application: Data Pipeline
- Real-world example of a data processing pipeline
- Multi-stage pipeline with generators, processors, and aggregators
- Worker pool for parallel processing
- Error handling in a production-like scenario
- Channels are typed conduits for communication between goroutines
- Unbuffered channels block until both sender and receiver are ready
- Buffered channels only block when the buffer is full
- The select statement allows you to wait on multiple channel operations
- Proper error handling and timeout mechanisms are essential for robust concurrent programs
main.go- Main program that runs all exampleschannelpatterns/- Directory containing individual pattern demonstrations:basic.go- Basic channel operationsbuffered.go- Buffered vs unbuffered channelssync.go- Synchronization patternsselect.go- Select statement exampleserrors.go- Error handling patternspractical_example.go- Real-world data pipeline examplelearning_mode.go- Step-by-step explanations for learning mode