0
0
Goprogramming~10 mins

Goroutine creation - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Goroutine creation
Start main function
Create goroutine
Goroutine runs concurrently
Main continues execution
Wait or finish main
Program ends
The main function starts, creates a goroutine that runs concurrently, then continues. The program ends when main finishes.
Execution Sample
Go
package main
import "fmt"
func sayHello() {
    fmt.Println("Hello from goroutine")
}
func main() {
    go sayHello()
    fmt.Println("Hello from main")
}
This code creates a goroutine to run sayHello concurrently while main prints its own message.
Execution Table
StepActionEvaluationResult
1Start main functionmain startsReady to run code
2Create goroutine with go sayHello()sayHello scheduledsayHello runs concurrently
3Execute fmt.Println("Hello from main")Print main messageOutput: Hello from main
4Goroutine runs sayHello()Print goroutine messageOutput: Hello from goroutine
5Main function endsProgram endsProgram terminates
💡 Main function ends, program terminates, goroutine may finish before or after main prints
Variable Tracker
VariableStartAfter goroutine creationAfter main printFinal
sayHello goroutineNot startedScheduled to runRunning or finishedFinished
main functionRunningRunningRunningFinished
Key Moments - 2 Insights
Why might the goroutine's print appear before or after the main print?
Because goroutines run concurrently, their execution order is not guaranteed. See execution_table steps 3 and 4 where outputs can appear in any order.
What happens if main ends before the goroutine runs?
The program ends immediately, stopping all goroutines. So the goroutine might not print if main finishes too soon, as shown in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed at step 3?
AHello from goroutine
BHello from main
CNo output
DProgram ends
💡 Hint
Check the 'Result' column at step 3 in execution_table
At which step does the goroutine start running?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
Look for when sayHello prints in execution_table
If main ends immediately after creating the goroutine, what might happen?
AGoroutine might not run at all
BGoroutine definitely prints first
CProgram waits for goroutine to finish
DMain prints twice
💡 Hint
See key_moments about program ending before goroutine runs
Concept Snapshot
Goroutine creation in Go:
- Use 'go' keyword before a function call
- Starts function concurrently
- Main continues without waiting
- Program ends when main finishes
- Goroutine may not complete if main ends early
Full Transcript
This example shows how a goroutine is created in Go using the 'go' keyword before a function call. The main function starts and creates a goroutine that runs the sayHello function concurrently. The main function then prints its own message. Because goroutines run at the same time as main, their output order can vary. If main ends before the goroutine runs, the program stops immediately and the goroutine may not print. This trace shows each step: starting main, creating the goroutine, printing from main, printing from the goroutine, and ending the program.