0
0
Goprogramming~15 mins

Goroutine lifecycle - Mini Project: Build & Apply

Choose your learning style9 modes available
Goroutine lifecycle
📖 Scenario: You are building a simple Go program to understand how goroutines work. Goroutines are like lightweight helpers that run tasks at the same time as your main program. This helps your program do many things without waiting for each one to finish.
🎯 Goal: You will create a program that starts a goroutine, lets it run, and then shows when it finishes. This will help you see the lifecycle of a goroutine from start to end.
📋 What You'll Learn
Create a channel to communicate between goroutine and main function
Start a goroutine that sends a message when done
Use a variable to hold the message from the goroutine
Print the message received from the goroutine
💡 Why This Matters
🌍 Real World
Goroutines help programs do many tasks at the same time, like handling multiple users or downloading files without waiting.
💼 Career
Understanding goroutine lifecycle is key for Go developers to write efficient, concurrent programs used in web servers, cloud services, and more.
Progress0 / 4 steps
1
Create a channel to communicate
Create a channel called done that can send and receive strings using make(chan string).
Go
Hint

Use make(chan string) to create a channel for strings.

2
Start a goroutine that sends a message
Start a goroutine using go keyword that sends the string "Goroutine finished" to the done channel.
Go
Hint

Use go func() { ... }() to start a goroutine and send the message to done.

3
Receive the message from the goroutine
Create a variable called msg and receive the string from the done channel using msg := <-done.
Go
Hint

Use msg := <-done to receive the message from the channel.

4
Print the message received from the goroutine
Use fmt.Println(msg) to print the message stored in msg.
Go
Hint

Use fmt.Println(msg) to show the message on the screen.