0
0
Goprogramming~10 mins

Writing first Go program - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Writing first Go program
Start program
Import fmt package
Define main function
Execute fmt.Println("Hello, world!")
Print output to screen
Program ends
The program starts, imports the fmt package for printing, runs the main function, prints the message, then ends.
Execution Sample
Go
package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}
This program prints 'Hello, world!' to the screen.
Execution Table
StepActionEvaluationResult
1Start programN/AProgram begins execution
2Import fmt packageN/Afmt package loaded for printing
3Define main functionN/Amain function ready to run
4Call fmt.Println with "Hello, world!"fmt.Println("Hello, world!")Prints Hello, world! to screen
5End main functionN/AProgram finishes
💡 Program ends after printing the message.
Variable Tracker
VariableStartAfter Step 4Final
N/AN/AN/AN/A
Key Moments - 3 Insights
Why do we need to import the fmt package?
The fmt package provides the Println function used to print text. Without importing it, the program cannot print anything. See step 2 in the execution table.
What is the main function for?
The main function is the starting point of the program where execution begins. The code inside main runs first. See step 3 and 4 in the execution table.
Why does the program end after printing?
After the main function finishes running, the program has no more instructions and stops. See step 5 and exit note.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 4?
AThe program ends
BThe program imports the fmt package
CThe program prints 'Hello, world!' to the screen
DThe main function is defined
💡 Hint
Check the 'Action' and 'Result' columns at step 4 in the execution table.
At which step does the program finish running?
AStep 5
BStep 3
CStep 2
DStep 1
💡 Hint
Look at the 'Step' and 'Result' columns to find when the program ends.
If we remove the import statement, what will happen?
AThe program will print 'Hello, world!' anyway
BThe program will fail because fmt.Println is undefined
CThe program will run but print nothing
DThe program will end immediately
💡 Hint
Refer to the key moment about why importing fmt is necessary.
Concept Snapshot
Go program starts with package main
Import fmt package to print
Define main() function as entry point
Use fmt.Println() to print text
Program runs main, prints, then ends
Full Transcript
This Go program starts by declaring package main, which is required for executable programs. It imports the fmt package to use its Println function for printing text. The main function is defined as the program's entry point. Inside main, fmt.Println("Hello, world!") prints the message to the screen. After printing, the program ends because main finishes execution.