Why concurrency is needed
📖 Scenario: Imagine you run a small bakery. You have to bake bread, prepare cakes, and serve customers. Doing one task at a time makes customers wait longer. If you could do tasks at the same time, you would serve customers faster and keep them happy.
🎯 Goal: Build a simple Go program that shows how doing tasks one by one is slower than doing them concurrently. You will create tasks as functions, run them sequentially first, then run them concurrently using goroutines.
📋 What You'll Learn
Create three functions:
bakeBread(), prepareCake(), and serveCustomer() that each print a message and wait 2 seconds.Create a main function that calls these three functions one after another (sequentially).
Add a configuration variable
useConcurrency to switch between sequential and concurrent execution.Modify the main function to run the three tasks concurrently using goroutines if
useConcurrency is true.Print messages to show when each task starts and ends.
💡 Why This Matters
🌍 Real World
Concurrency is used in real life when you want to do many things at once, like cooking multiple dishes or serving many customers quickly.
💼 Career
Understanding concurrency is important for software developers to build fast and responsive programs, especially for web servers, games, and apps that handle many users.
Progress0 / 4 steps