This flow shows how a goroutine starts, runs alongside main, completes, and how the program ends when main finishes.
Execution Sample
Go
package main
import"fmt"
func main() {
go fmt.Println("Hello from goroutine")
fmt.Println("Hello from main")
}
This code starts a goroutine to print a message while main prints another message concurrently.
Execution Table
Step
Action
Goroutine State
Main State
Output
1
Start main function
Not started
Running
2
Launch goroutine with go keyword
Starting
Running
3
Main prints 'Hello from main'
Running concurrently
Printed message
Hello from main
4
Goroutine prints 'Hello from goroutine'
Printed message
Printed message
Hello from main
Hello from goroutine
5
Main function ends
May or may not have finished
Ended
Hello from main
Hello from goroutine
6
Program ends
Ended
Ended
Hello from main
Hello from goroutine
💡 Program ends when main function finishes, goroutine may finish before or after main ends.
Variable Tracker
Variable
Start
After Step 2
After Step 3
After Step 4
Final
Goroutine State
Not started
Starting
Running concurrently
Printed message
Ended
Main State
Running
Running
Printed message
Printed message
Ended
Key Moments - 3 Insights
Why might the goroutine's output not appear before the program ends?
Because the main function can finish and end the program before the goroutine runs or completes, as shown in step 5 of the execution_table.
Does the goroutine run before or after the main function prints?
They run concurrently, so the order is not guaranteed. The execution_table shows main printing first at step 3, but goroutine could print first in other runs.
What happens if main ends before the goroutine finishes?
The program ends immediately, stopping all goroutines, so the goroutine may not complete its task, as explained in the exit_note.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the goroutine state at step 3?
AEnded
BNot started
CRunning concurrently
DStarting
💡 Hint
Check the 'Goroutine State' column in row for step 3 in execution_table.
At which step does the main function print its message?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' and 'Output' columns in execution_table for when main prints.
If main ends immediately after launching the goroutine, what might happen to the goroutine's output?
AIt might not print at all
BIt will always print before main ends
CIt will print twice
DIt will block main from ending
💡 Hint
Refer to the exit_note and key_moments about program ending before goroutine finishes.
Concept Snapshot
Goroutine lifecycle in Go:
- main starts and runs
- 'go' keyword launches goroutine concurrently
- goroutine runs alongside main
- main may finish before goroutine
- program ends when main ends, stopping goroutines
- use sync or channels to wait for goroutines
Full Transcript
This visual trace shows how a goroutine starts when the 'go' keyword is used inside main. The goroutine runs at the same time as main. Both can print messages in any order. The program ends when main finishes, which can stop goroutines if they are not done. This means goroutines may not complete if main ends too soon. To ensure goroutines finish, synchronization methods are needed. The execution table tracks states and outputs step by step.