0
0
Goprogramming~15 mins

Goroutine creation - Mini Project: Build & Apply

Choose your learning style9 modes available
Goroutine creation
📖 Scenario: You are building a simple Go program that runs tasks concurrently to save time, like having multiple helpers working at once.
🎯 Goal: Create a Go program that launches a goroutine to print a message concurrently with the main function.
📋 What You'll Learn
Create a function called sayHello that prints "Hello from goroutine!"
Launch sayHello as a goroutine from the main function
Add a short pause in main to allow the goroutine to finish before the program exits
💡 Why This Matters
🌍 Real World
Goroutines let programs do many things at once, like handling multiple users or tasks simultaneously.
💼 Career
Understanding goroutines is essential for Go developers working on fast, efficient, and concurrent applications.
Progress0 / 4 steps
1
Create the sayHello function
Write a function called sayHello that prints the text "Hello from goroutine!" using fmt.Println.
Go
Hint

Use func sayHello() to define the function and fmt.Println to print the message.

2
Launch sayHello as a goroutine
Inside the main function, start the sayHello function as a goroutine using the go keyword.
Go
Hint

Use go sayHello() inside main to start the goroutine.

3
Add a pause to let the goroutine finish
Import the time package and add time.Sleep(time.Second) inside main after launching the goroutine to pause the program for 1 second.
Go
Hint

Use import "time" and time.Sleep(time.Second) to pause the main function.

4
Run the program and see the output
Run the program and print the output. The program should print Hello from goroutine!.
Go
Hint

Run the program and check the console output.