Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Go Download Manager Tutorial

A concurrent download manager demonstration written in Go that showcases essential Go programming concepts including goroutines, channels, interfaces, generics, and mutex-based synchronization.

Overview

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.

Features

  • 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

Project Structure

.
├── main.go          # Main application code
├── go.mod           # Go module file
└── README.md        # This file

Key Components

DownloadJob Struct

Represents a file download task with:

  • ID: Unique identifier for the job
  • FileName: Name of the file being downloaded
  • SizeMB: Size of the file in megabytes
  • Status: Current status of the download

Functions

  • 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

Concurrency Features

  • 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

How It Works

  1. Job Creation: Creates three download jobs (movie.mp4, song.mp3, book.pdf)
  2. ID Generation: Uses a closure-based generator to assign unique IDs
  3. Concurrent Execution: Launches goroutines for each download job
  4. Processing: Each goroutine simulates a 2-second download process
  5. Result Handling: Downloads are marked as failed if file size > 50MB, otherwise succeed
  6. Statistics Tracking: Thread-safe map tracks completion and failure counts
  7. Result Collection: Main function collects results using a select statement

Getting Started

Prerequisites

  • Go 1.18+ (for generic function support)

Running the Project

# Clone the repository
git clone <repository-url>
cd GoTutorial

# Run the application
go run main.go

Expected Output

3
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]

Learning Outcomes

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

License

This is a tutorial project for educational purposes.

Author

Go Tutorial Project


Note: This is a learning project designed to teach Go concurrency patterns and core language features.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages