0
0
Goprogramming~5 mins

main package and main function in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the main package in Go?
The main package is special in Go because it defines an executable program. It tells the Go compiler to build a runnable application instead of a library.
Click to reveal answer
beginner
What is the role of the main function in a Go program?
The main function is the entry point of a Go program. When you run the program, Go starts executing from this function.
Click to reveal answer
beginner
Can a Go program have multiple main functions?
No, a Go program can have only one main function inside the main package. This function is where the program starts running.
Click to reveal answer
intermediate
What happens if you try to run a Go program without a main function in the main package?
The program will not compile. Go requires a main function in the main package to know where to start execution.
Click to reveal answer
beginner
How do you define the main package and main function in Go? Show a simple example.
You write package main at the top, then define func main() { }. For example:<br>
package main

func main() {
    println("Hello, world!")
}
Click to reveal answer
What is the special package name that makes a Go program executable?
Arun
Bexec
Cmain
Dstart
Where does a Go program start running?
AIn the <code>init</code> function
BIn the <code>main</code> function
CIn the <code>start</code> function
DIn the <code>run</code> function
Can you have multiple main functions in one Go program?
ANo, only one <code>main</code> function is allowed
BYes, as many as you want
COnly if they are in different packages
DOnly if they have different names
What happens if the main function is missing in the main package?
AThe program runs and exits immediately
BThe program runs but does nothing
CThe program runs and calls <code>init</code> instead
DThe program will not compile
Which of these is the correct way to start a Go program?
Apackage main func main() {}
Bpackage main func start() {}
Cpackage exec func main() {}
Dpackage run func main() {}
Explain why the main package and main function are important in Go programs.
Think about how Go knows where to start running your code.
You got /4 concepts.
    Write a simple Go program that prints "Hello, Go!" using the main package and main function.
    Start with package main, then define func main, then print the message.
    You got /3 concepts.