Shows how the main function starts, launches a goroutine that runs concurrently, and both run independently until completion.
Execution Sample
Go
package main
import"fmt"
func main() {
go fmt.Println("Hello from goroutine")
fmt.Println("Hello from main")
}
This Go program runs two print statements concurrently: one in a goroutine and one in the main function.
Execution Table
Step
Action
Goroutine State
Main State
Output
1
Start main function
Not started
Running
2
Launch goroutine with go keyword
Started (concurrent)
Running
3
Main prints "Hello from main"
Running concurrently
Printed "Hello from main"
Hello from main
4
Goroutine prints "Hello from goroutine"
Printed "Hello from goroutine"
Running
Hello from goroutine
5
Main function ends
May still be running or finished
Ended
6
Program ends when main exits
Ends if not finished
Ends
💡 Program ends when main function finishes, goroutine may or may not finish if main exits too early.
Variable Tracker
Variable
Start
After Step 2
After Step 3
After Step 4
Final
main
Running
Running
Printed output
Printed output
Ended
goroutine
Not started
Started
Running
Printed output
May end or still running
Key Moments - 3 Insights
Why might the goroutine's output not appear before the program ends?
Because the main function can finish and exit the program before the goroutine runs or completes, as shown in step 6 of the execution_table.
Does the goroutine block the main function from continuing?
No, the goroutine runs concurrently and does not block main, so main continues immediately after launching the goroutine (step 3).
How do we know the goroutine started?
At step 2, the goroutine is launched with the go keyword, changing its state from 'Not started' to 'Started (concurrent)' in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after step 3?
ANo output yet
B"Hello from main"
C"Hello from goroutine"
DBoth outputs printed
💡 Hint
Check the Output column at step 3 in the execution_table.
At which step does the goroutine print its message?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the Output and Goroutine State columns in the execution_table.
If the main function ends immediately after launching the goroutine, what might happen?
AGoroutine output always appears
BProgram waits for goroutine to finish
CGoroutine may not run or finish before program ends
DMain function blocks until goroutine finishes
💡 Hint
Refer to the exit_note and step 6 in the execution_table.
Concept Snapshot
Go's concurrent execution model uses goroutines.
Use 'go' keyword to start a goroutine.
Goroutines run independently and concurrently.
Main function does not wait for goroutines by default.
Program ends when main finishes, possibly before goroutines.
Use synchronization to wait for goroutines if needed.
Full Transcript
This example shows how Go runs code concurrently using goroutines. The main function starts and launches a goroutine with the 'go' keyword. Both the main function and the goroutine run at the same time. The main function prints its message and continues. The goroutine prints its message independently. The program ends when the main function finishes, which might happen before the goroutine completes. This means the goroutine's output might not always appear if the main function exits too soon. Goroutines do not block the main function. To ensure goroutines finish, synchronization methods are needed.