📖 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
1
Create task functions
Create three functions called bakeBread(), prepareCake(), and serveCustomer(). Each function should print a start message, wait for 2 seconds using time.Sleep(2 * time.Second), then print a done message.
Go
Hint
Use fmt.Println to print messages and time.Sleep to wait.
2
Add concurrency switch variable
Create a boolean variable called useConcurrency and set it to false. This will control if tasks run one by one or at the same time.
Go
Hint
Write useConcurrency := false inside main().
3
Run tasks sequentially or concurrently
In the main() function, write an if statement that checks if useConcurrency is false. If so, call bakeBread(), prepareCake(), and serveCustomer() one after another. Otherwise, run these three functions concurrently using goroutines and wait for all to finish using a sync.WaitGroup.
Go
Hint
Use sync.WaitGroup to wait for goroutines to finish when useConcurrency is true.
4
Print results and test concurrency
Add fmt.Println("Running tasks sequentially") before the sequential calls and fmt.Println("Running tasks concurrently") before the concurrent goroutines. Run the program twice: once with useConcurrency set to false and once with it set to true. Observe the difference in output timing.
Go
Hint
Print messages before running tasks to show if they run sequentially or concurrently.
Practice
(1/5)
1. Why do we need concurrency in Go programs?
easy
A. To simplify the code by removing all loops and conditions.
B. To allow multiple tasks to run at the same time, improving speed and responsiveness.
C. To make the program use less memory by running tasks one after another.
D. To avoid using functions and write everything in the main block.
Solution
Step 1: Understand concurrency purpose
Concurrency allows a program to handle multiple tasks at once instead of waiting for one to finish before starting another.
Step 2: Relate concurrency to Go
Go uses goroutines to run tasks concurrently, which helps programs be faster and more responsive.
Final Answer:
To allow multiple tasks to run at the same time, improving speed and responsiveness. -> Option B
Quick Check:
Concurrency = multiple tasks at once [OK]
Hint: Concurrency means doing many things at once [OK]
Common Mistakes:
Thinking concurrency reduces memory by running tasks sequentially
Confusing concurrency with simpler code structure
Believing concurrency removes the need for functions
2. Which of the following is the correct way to start a goroutine in Go?
easy
A. go myFunction()
B. start myFunction()
C. run myFunction()
D. goroutine myFunction()
Solution
Step 1: Recall Go syntax for goroutines
In Go, the keyword go is used before a function call to start it as a goroutine.
Step 2: Check each option
Only go myFunction() uses the correct go keyword syntax; others are invalid in Go.
Final Answer:
go myFunction() -> Option A
Quick Check:
Start goroutine with 'go' keyword [OK]
Hint: Use 'go' before function to start goroutine [OK]
Common Mistakes:
Using 'start' or 'run' instead of 'go'
Forgetting the 'go' keyword before function call
Trying to declare goroutine with 'goroutine' keyword
The function sayHello runs as a goroutine, so it runs concurrently with main. The main function sleeps 1 second to allow sayHello to print.
Step 2: Determine print order
Because of the sleep, "Hello" prints first from the goroutine, then "World" prints after sleep ends.
Final Answer:
Hello
World -> Option C
Quick Check:
Goroutine prints 'Hello' before main prints 'World' [OK]
Hint: Sleep lets goroutine finish before main prints [OK]
Common Mistakes:
Ignoring time.Sleep and missing 'Hello' output
Assuming 'World' prints before 'Hello'
Thinking goroutine blocks main function
4. Identify the problem in this Go code snippet:
package main
import "fmt"
func main() {
go func() {
fmt.Println("Running")
}()
fmt.Println("Done")
}
medium
A. Goroutines cannot be used inside main function.
B. The anonymous function syntax is incorrect.
C. The fmt package is not imported properly.
D. The program may exit before the goroutine runs, so "Running" might not print.
Solution
Step 1: Analyze goroutine execution timing
The goroutine runs concurrently, but main may finish and exit before the goroutine prints "Running".
Step 2: Check code correctness
The anonymous function syntax and imports are correct; goroutines can run inside main.
Final Answer:
The program may exit before the goroutine runs, so "Running" might not print. -> Option D
Quick Check:
Program exit can stop goroutine early [OK]
Hint: Main may exit before goroutine finishes [OK]
Common Mistakes:
Thinking anonymous function syntax is wrong
Believing goroutines can't run in main
Ignoring program exit timing
5. You want to download 3 files concurrently in Go and print "All done" only after all downloads finish. Which approach correctly uses concurrency to achieve this?
hard
A. Start 3 goroutines for downloads and use a sync.WaitGroup to wait for all to finish before printing "All done".
B. Call the download functions one after another without goroutines, then print "All done".
C. Start 3 goroutines but print "All done" immediately after starting them without waiting.
D. Use a for loop to start downloads but do not synchronize; rely on time.Sleep to wait.
Solution
Step 1: Understand concurrency with synchronization
Starting goroutines runs downloads concurrently, but we must wait for all to finish before printing "All done".
Step 2: Use sync.WaitGroup for waiting
sync.WaitGroup lets main wait until all goroutines complete, ensuring correct order.
Final Answer:
Start 3 goroutines for downloads and use a sync.WaitGroup to wait for all to finish before printing "All done". -> Option A
Quick Check:
WaitGroup synchronizes goroutines before final print [OK]
Hint: Use WaitGroup to wait for all goroutines [OK]
Common Mistakes:
Not waiting for goroutines before printing
Using sleep instead of proper synchronization
Running downloads sequentially without concurrency
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.