A concurrent download manager demonstration written in Go that showcases essential Go programming concepts including goroutines, channels, interfaces, generics, and mutex-based synchronization.
This project simulates a file download system that processes multiple download jobs concurrently. It demonstrates how to handle concurrent operations safely in Go, manage statistics across goroutines, and use channels for inter-goroutine communication.
- Concurrent Downloads: Uses goroutines to simulate multiple file downloads running in parallel
- Channel-based Communication: Separate success and failure channels for handling download results
- Thread-safe Statistics: Implements mutex-based locking for safe concurrent map access
- Closure-based ID Generator: Demonstrates Go's function closures for state management
- Interface Implementation: Shows how to implement and use interfaces with structs
- Generic Functions: Utilizes Go's generic programming features for type-safe operations
- Struct Methods: Demonstrates receiver methods for structs
.
├── main.go # Main application code
├── go.mod # Go module file
└── README.md # This file
Represents a file download task with:
ID: Unique identifier for the jobFileName: Name of the file being downloadedSizeMB: Size of the file in megabytesStatus: Current status of the download
- IdGenerator(): Returns a closure that generates sequential unique IDs
- Download(): Simulates file download with concurrency and failure handling based on file size
- PrintAll(): Generic function to print any slice of any type
- logger(): Utility function for logging messages
- Goroutines: Each download job runs in its own goroutine
- Channels: Two buffered channels handle success and failure results
- Mutex: Protects the statistics map from concurrent access
- Select Statement: Uses select to receive results from multiple channels
- Job Creation: Creates three download jobs (movie.mp4, song.mp3, book.pdf)
- ID Generation: Uses a closure-based generator to assign unique IDs
- Concurrent Execution: Launches goroutines for each download job
- Processing: Each goroutine simulates a 2-second download process
- Result Handling: Downloads are marked as failed if file size > 50MB, otherwise succeed
- Statistics Tracking: Thread-safe map tracks completion and failure counts
- Result Collection: Main function collects results using a select statement
- Go 1.18+ (for generic function support)
# Clone the repository
git clone <repository-url>
cd GoTutorial
# Run the application
go run main.go3
3
movie.mp4
Processing movie.mp4
song.mp3
Processing song.mp3
book.pdf
Processing book.pdf
map[completed:0 failed:0]
Download Started
[{1 movie.mp4 100 pending} {2 song.mp3 20 pending} {3 book.pdf 5 pending}]
Processing movie.mp4
Processing song.mp3
Processing book.pdf
Starting movie.mp4
Starting song.mp3
Starting book.pdf
Failed : movie.mp4
Success : song.mp3
Success : book.pdf
Finished movie.mp4
Finished song.mp3
Finished book.pdf
Final Stats:
map[completed:2 failed:1]
This project demonstrates:
- ✅ Creating and working with structs and methods
- ✅ Implementing interfaces in Go
- ✅ Using goroutines for concurrent execution
- ✅ Channel-based communication between goroutines
- ✅ Mutex synchronization for shared resources
- ✅ Generic functions and type parameters
- ✅ Closures and higher-order functions
- ✅ Defer statements for cleanup
- ✅ Select statement for multiplexing channels
This is a tutorial project for educational purposes.
Go Tutorial Project
Note: This is a learning project designed to teach Go concurrency patterns and core language features.