0
0
Goprogramming~30 mins

Concurrent execution model in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Concurrent Execution Model in Go
📖 Scenario: You are building a simple program to understand how Go runs tasks at the same time using goroutines. Imagine you want to greet three friends, but you want to say hello to each one without waiting for the others.
🎯 Goal: Learn how to start multiple tasks concurrently using goroutines and see their greetings printed in any order.
📋 What You'll Learn
Create a function that prints a greeting message
Start multiple goroutines to run the greeting function concurrently
Use a channel to wait for all goroutines to finish
Print all greetings without waiting for each one sequentially
💡 Why This Matters
🌍 Real World
Concurrent execution helps programs do many things at once, like handling multiple users or tasks without waiting.
💼 Career
Understanding goroutines and channels is essential for Go developers working on fast, efficient, and scalable software.
Progress0 / 4 steps
1
Create a function to print greetings
Write a function called greet that takes a string parameter name and prints "Hello, " followed by the name. Use fmt.Println inside the function.
Go
Hint

Use fmt.Println("Hello, " + name) inside the greet function to print the greeting.

2
Create a channel to wait for goroutines
Inside main, create a channel called done of type chan bool to signal when a goroutine finishes.
Go
Hint

Use done := make(chan bool) to create the channel.

3
Start goroutines to greet friends concurrently
In main, start three goroutines using go greet(name) for "Alice", "Bob", and "Charlie". After each greeting, send true to the done channel to signal completion.
Go
Hint

Use anonymous functions with go func() { ... }() to call greet and then send true to done.

4
Wait for all goroutines to finish and print greetings
In main, add a for loop that runs three times. Each time, receive from the done channel to wait for a goroutine to finish. This ensures all greetings are printed before the program ends.
Go
Hint

Use for i := 0; i < 3; i++ { <-done } to wait for all goroutines.