Understanding the main package and main function in Go
📖 Scenario: You want to create a simple Go program that prints a greeting message. This program will help you learn how to set up the main package and main function, which are essential to run any Go program.
🎯 Goal: Build a Go program that uses the main package and main function to print Hello, Go! to the screen.
📋 What You'll Learn
Create a Go file with the main package
Define the main function
Use fmt.Println to print the greeting message
💡 Why This Matters
🌍 Real World
Every Go program needs a main package and main function to run. This is how Go knows where to start your program.
💼 Career
Understanding the main package and main function is essential for any Go developer, as it is the foundation of all Go applications.
Progress0 / 4 steps
1
Create the main package
Write package main at the top of your Go file to declare the main package.
Go
Hint
The main package is the starting point of a Go program.
2
Import the fmt package
Add import "fmt" below the package main line to use the formatting package for printing.
Go
Hint
The fmt package lets you print text to the screen.
3
Define the main function
Write the func main() function below the import statement. This function is where your program starts running.
Go
Hint
The main function is special: Go runs it first.
4
Print the greeting message
Inside the main function, write fmt.Println("Hello, Go!") to print the message to the screen.
Go
Hint
Use fmt.Println to show text on the screen.
Practice
(1/5)
1. What is the purpose of the main package in a Go program?
easy
A. It contains only variable declarations.
B. It is used to import external libraries only.
C. It defines the entry point where the program starts running.
D. It is used to define custom data types.
Solution
Step 1: Understand the role of the main package
The main package is special in Go because it tells the compiler this is an executable program, not a library.
Step 2: Identify the program start point
Within the main package, the main function is the starting point where execution begins.
Final Answer:
It defines the entry point where the program starts running. -> Option C
Quick Check:
main package = program entry [OK]
Hint: Remember: main package means program starts here [OK]
Common Mistakes:
Thinking main package is only for imports
Confusing main package with libraries
Assuming main package holds only variables
2. Which of the following is the correct way to declare the main function in Go?
easy
A. func main() {}
B. function main() {}
C. def main() {}
D. main func() {}
Solution
Step 1: Recall Go function syntax
In Go, functions are declared with the keyword func, followed by the function name and parentheses.
Step 2: Check the main function declaration
The main function must be declared as func main() {} with no parameters and no return values.
Final Answer:
func main() {} -> Option A
Quick Check:
Go functions start with func [OK]
Hint: Go functions start with 'func' keyword [OK]
Common Mistakes:
Using 'function' instead of 'func'
Using Python or JavaScript syntax
Placing 'func' after the function name
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
medium
A. fmt.Println("Hello, Go!")
B. No output, program error
C. package main
D. Hello, Go!
Solution
Step 1: Understand the program structure
The program imports the fmt package and calls fmt.Println inside main to print a string.
Step 2: Predict the output
The fmt.Println function prints the string with a newline, so the output is exactly: Hello, Go!
Final Answer:
Hello, Go! -> Option D
Quick Check:
fmt.Println prints text [OK]
Hint: fmt.Println prints text to console [OK]
Common Mistakes:
Expecting code to print the code itself
Confusing package name with output
Thinking program will error without main
4. Identify the error in this Go program:
package main
func main() {
fmt.Println("Hello")
}
medium
A. main function must return a value
B. Missing import of fmt package
C. package name should not be main
D. fmt.Println syntax is incorrect
Solution
Step 1: Check for required imports
The program uses fmt.Println but does not import the fmt package, which is required to use it.
Step 2: Understand Go import rules
Without importing fmt, the compiler will raise an error because fmt is undefined.
Final Answer:
Missing import of fmt package -> Option B
Quick Check:
Use import for external packages [OK]
Hint: Always import packages before using them [OK]
Common Mistakes:
Forgetting to import fmt
Thinking main must return a value
Changing package main to something else
5. You want to create a Go program that prints "Welcome!" only when run directly, but not when imported as a package. Which setup is correct?
hard
A. Use package main and define func main() { fmt.Println("Welcome!") }
B. Use package main but define func welcome() { fmt.Println("Welcome!") }
C. Use any package and define func main() { fmt.Println("Welcome!") }
D. Use package main and define func init() { fmt.Println("Welcome!") }
Solution
Step 1: Understand package main role
Only programs with package main and a main function are executable directly.
Step 2: Identify correct function to run on start
The main function is the entry point; code inside it runs only when program runs directly.
Step 3: Compare options
Use package main and define func main() { fmt.Println("Welcome!") } uses package main and main function correctly. Use package main but define func welcome() { fmt.Println("Welcome!") } defines a different function that won't run automatically. Use any package and define func main() { fmt.Println("Welcome!") } uses any package, which won't create an executable. Use package main and define func init() { fmt.Println("Welcome!") } uses init(), which runs on import too.
Final Answer:
Use package main and define func main() { fmt.Println("Welcome!") } -> Option A
Quick Check:
package main + main() = executable start [OK]
Hint: Only package main with main() runs directly [OK]
Common Mistakes:
Using init() instead of main() for start
Using non-main package for executable
Defining functions not named main
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.